Jquery Ajax 返回 400 错误请求

3

我试图将数据发送到本地数据库服务器,但每次尝试发送时都会收到400错误请求错误。

var studentEmail = "ali@gmail.com";
            var dataString = '&questionNumber='+ temp + '&answer='+ value + '&email='+ studentEmail;


            $.ajax({
            type: "POST",
            dataType:'json',
            url: "js/dbcon.php",
            data: JSON.stringify(dataString),
            processData: false,
            contentType: "application/json; charset=utf-8"
            });

这是 PHP 文件。

<?php
$connection = mysql_connect("127.0.0.1", "root", "root"); // Establishing Connection with Server..
$db = mysql_select_db("db", $connection); // Selecting Database
//Fetching Values from URL
$questionNumber=$_POST['questionNumber'];
$answer=$_POST['answer'];
$email=$_POST['email'];
//Insert query
$query = mysql_query("INSERT INTO answers (questionNumber,studentAnswer,studentEmail) VALUES ($questionNumber,$answer,$email)");
echo "succesfully posted";
mysql_close($connection); // Connection Closed
?>

有人能看出我做错了什么吗?

谢谢提前!


如果我将POST更改为GET,它可以工作,但无法将数据插入到我的数据库中。 - undefined
你必须转义字符串 @ ,因为它在 URL 中是无效的。 - undefined
这不好:你没有发送键值对,所以服务器上将无法获取任何来自$_POST的数据,你没有返回json,所以你的ajax调用永远不会成功完成,你存在SQL注入问题,并且你使用了已弃用的mysql API。 - undefined
我确保了php文件在js文件夹中。 - undefined
每次你在新代码中使用mysql_数据库扩展时,世界上某个地方就有一只小猫被勒死了。这个扩展已经被弃用多年,并且在PHP7中已经彻底消失了。 如果你正在学习PHP,最好将精力放在学习PDOmysqli数据库扩展和预处理语句上。 从这里开始:http://php.net/manual/zh/book.pdo.php - undefined
显示剩余2条评论
4个回答

2

JSON.stringify()方法用于将JavaScript object转换为JSON字符串。

因此,dataString变量必须是JavaScript object

var data ={questionNumber:temp ,answer: value ,email:studentEmail};

AJAX

$.ajax({
    type: "POST",
    dataType:'json',
    url: "js/dbcon.php",
    data: JSON.stringify(data),
    processData: false,
    contentType: "application/json; charset=utf-8"
});

谢谢您的回复,我按照您说的进行了更改,但现在出现了404错误。我还确保php文件在js文件夹中。 - undefined
我认为它找不到你的文件,但如果它找到了文件,我的更改应该可以起作用。 - undefined
@RiggsFolly,你说的是不正确的,因为你使用了contentType: "application/json; charset=utf-8",这代表了你发送的数据类型。 - undefined

1
如果你将提交方式从POST改为GET,那么你需要在你的PHP文件中将$_POST替换成$_GET。

1
有一种更简单的方法可以传递数据,既适用于POST请求,也适用于GET请求。
var studentEmail = "ali@gmail.com";
$.ajax({
        type: "POST",
        dataType:'json',
        url: "js/dbcon.php",
        data: {questionNumber:temp, answer:value, email: studentEmail},
        processData: false,

});

现在jQuery会完成所有繁重的工作,并将充满数据的对象转换为POST或GET请求所需的任何格式。

1
你可以这样发送 ajax 请求:

var studentEmail = "ali@gmail.com";
            $.ajax({
            type: "POST",
            dataType:'json',
            url: "js/dbcon.php",
            data: ({'questionNumber':temp,'answer':value, 'email':studentEmail }),
            processData: false,
            contentType: "application/json; charset=utf-8"
            });

此外,PHP文件还需要返回一个json字符串。
echo "succesfully posted";

这不是一个有效的json响应。

返回类似于以下内容:

$arr = array('success' => true, 'answer' => "succesfully posted");

echo json_encode($arr);

此外,也可以在这里查看:http://php.net/manual/de/function.json-encode.php

在将数据插入数据库之前,您应该验证输入数据。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接