Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ajax返回400个错误请求_Ajax_Jquery - Fatal编程技术网

ajax返回400个错误请求

ajax返回400个错误请求,ajax,jquery,Ajax,Jquery,这很好: jQuery('#my_get_related_keywords').click(function() { if (jQuery('#my_keyword').val() == '') return false; jQuery.getJSON("http://boss.yahooapis.com/ysearch/web/v1/" +jQuery('#my_keyword').val()+"?" +"appid=myAppID"

这很好:

jQuery('#my_get_related_keywords').click(function() {
    if (jQuery('#my_keyword').val() == '') return false;
        jQuery.getJSON("http://boss.yahooapis.com/ysearch/web/v1/"
        +jQuery('#my_keyword').val()+"?"
        +"appid=myAppID"
        +"&lang=en"
        +"&format=json"
        +"&count=50"
        +"&view=keyterms"
        +"&callback=?",
        function (data) {//do something}
这将返回400个错误请求(只是使用.ajax对上述jQuery进行了重新格式化,以支持错误处理)


将此添加到ajax调用中:

contentType: "application/json; charset=utf-8",
dataType: "json"

我想您只需要再添加两个选项(
contentType
dataType
):


迟来的回答,但我认为值得不断更新。扩展Andrea Turri answer以反映更新的jQuery API和.success/.error弃用方法

从jQuery 1.8开始。*执行此操作的首选方法是使用.done()和.fail()

e、 g


例如,请确保在$.ajax调用中始终使用“get”或“post”

$.ajax({
    type: 'get',
必须会见

app.post('/', function(req, res) {
app.get('/',函数(req,res){

=============== 邮递

必须会见

app.post('/', function(req, res) {

即使在设置了以下内容后,我仍收到400错误请求错误:

contentType: "application/json",
dataType: "json"
问题在于在json对象中传递的属性类型,对于ajax请求对象中的
数据
属性。
为了解决这个问题,我添加了一个错误处理程序,然后将错误记录到控制台。控制台日志将清楚地显示属性的验证错误(如果有)

这是我最初的代码:

var data = {
    "TestId": testId,
    "PlayerId": parseInt(playerId),
    "Result": result
};

var url = document.location.protocol + "//" + document.location.host + "/api/tests"
$.ajax({
    url: url,
    method: "POST",
    contentType: "application/json",
    data: JSON.stringify(data), // issue with a property type in the data object
    dataType: "json",
    error: function (e) {
        console.log(e); // logging the error object to console
    },
    success: function () {
        console.log('Success saving test result');
    }
});
现在,在发出请求后,我检查了浏览器开发工具中的console选项卡。
看起来是这样的:

responseJSON.errors[0]
清楚地显示了一个验证错误:JSON值无法转换为System.String.Path:$.TestId,这意味着我必须在发出请求之前将
TestId
转换为数据对象中的字符串

如下所示更改数据对象创建,解决了我的问题:

var data = {
        "TestId": String(testId), //converting testId to a string
        "PlayerId": parseInt(playerId),
        "Result": result
};

我假设通过记录和检查错误对象也可以识别其他可能的错误。

您是否必须在某个地方添加方法(post或get)?谢谢。就是这样。@Toro刚刚击败了您:)
contentType: "application/json",
dataType: "json"
var data = {
    "TestId": testId,
    "PlayerId": parseInt(playerId),
    "Result": result
};

var url = document.location.protocol + "//" + document.location.host + "/api/tests"
$.ajax({
    url: url,
    method: "POST",
    contentType: "application/json",
    data: JSON.stringify(data), // issue with a property type in the data object
    dataType: "json",
    error: function (e) {
        console.log(e); // logging the error object to console
    },
    success: function () {
        console.log('Success saving test result');
    }
});
var data = {
        "TestId": String(testId), //converting testId to a string
        "PlayerId": parseInt(playerId),
        "Result": result
};