jQuery.ajax 返回 400 Bad Request

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4156991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:46:01  来源:igfitidea点击:

jQuery.ajax returns 400 Bad Request

ajaxjquery

提问by Scott B

This works fine:

这工作正常:

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}

This returns 400 Bad Request (Just a reformulation of the above jQuery using .ajax to support error handling).

这将返回 400 Bad Request(只是使用 .ajax 支持错误处理的上述 jQuery 的重构)。

jQuery('#my_get_related_keywords').click(function()
    {
    if (jQuery('#my_keyword').val() == '') return false; 
    jQuery('#my_loader').show();
    jQuery.ajax(
        {
        url: "http://boss.yahooapis.com/ysearch/web/v1/"
        +jQuery('#my_keyword').val()+"?"
        +"appid=myAppID"
        +"&lang=en"
        +"&format=json"
        +"&count=50"
        +"&view=keyterms"
        +"&callback=?", 
        success: function(data)
            {//do something}

回答by Andrea Turri

I think you just need to add 2 more options (contentTypeand dataType):

我认为您只需要再添加 2 个选项(contentTypedataType):

$('#my_get_related_keywords').click(function() {

    $.ajax({
            type: "POST",
            url: "HERE PUT THE PATH OF YOUR SERVICE OR PAGE",
            data: '{"HERE YOU CAN PUT DATA TO PASS AT THE SERVICE"}',
            contentType: "application/json; charset=utf-8", // this
            dataType: "json", // and this
            success: function (msg) {
               //do something
            },
            error: function (errormessage) {
                //do something else
            }
        });
}

回答by light

Add this to your ajax call:

将此添加到您的 ajax 调用中:

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

回答by bhietpas

Late answer, but I figured it's worth keeping this updated. Expanding on Andrea Turri answer to reflect updated jQuery API and .success/.error deprecated methods.

迟到的答案,但我认为值得更新。扩展 Andrea Turri 的答案以反映更新的 jQuery API 和 .success/.error 不推荐使用的方法。

As of jQuery 1.8.* the preferred way of doing this is to use .done() and .fail(). Jquery Docs

从 jQuery 1.8.* 开始,这样做的首选方法是使用 .done() 和 .fail()。jQuery 文档

e.g.

例如

$('#my_get_related_keywords').click(function() {

    var ajaxRequest = $.ajax({
        type: "POST",
        url: "HERE PUT THE PATH OF YOUR SERVICE OR PAGE",
        data: '{"HERE YOU CAN PUT DATA TO PASS AT THE SERVICE"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json"});

    //When the request successfully finished, execute passed in function
    ajaxRequest.done(function(msg){
           //do something
    });

    //When the request failed, execute the passed in function
    ajaxRequest.fail(function(jqXHR, status){
        //do something else
    });
});

回答by Nelles

Be sure and use 'get' or 'post' consistantly with your $.ajax call for example.

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

$.ajax({
    type: 'get',

must be met with

必须遇到

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

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

=============== and for post

================ 和帖子

$.ajax({
    type: 'post',
$.ajax({
    type: 'post',

must be met with

必须遇到

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