jQuery ajax jquery总是运行错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5744760/
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 19:45:15  来源:igfitidea点击:

ajax jquery always running Error

jquery

提问by David

Everytime I run my ajax jquery function I get an error, this goes for all my ajax calls. here is an example of my code

每次我运行我的 ajax jquery 函数时,我都会收到一个错误,这适用于我所有的 ajax 调用。这是我的代码示例

 function FindContact(CompanyName,DivisionName,FirstName,LastName) {

        $.ajax({
            url: 'Path',
            dataType: "json",
            async:false,
            type:'post',
            data: {'FirstName':FirstName,'LastName':LastName,'DivisionName':DivisionName,'CompanyName':CompanyName},
            success: DisplayContacts,
            error: ErrorMsg
        });
    }

to get around this I use this

为了解决这个问题,我用这个

 function ErrorMsg(result) {
        if (result.status == 200 && result.statusText == 'OK') {
            DisplayContacts(result);
        }
        else {
            alert("FAILED : " + result.status + ' ' + result.statusText);
        }
    }

this is tough because I need to create method like this for every ajax request.

这很难,因为我需要为每个 ajax 请求创建这样的方法。

why does it run the error code 1st???

为什么它运行错误代码 1 ???

Please help!

请帮忙!

回答by locrizak

Ensure that what you are returning is valid json. If its not, and everything goes correct on the server, $.ajax will go to error rather than success.

确保您返回的内容是有效的 json。如果不是,并且服务器上一切正常,$.ajax 将出错而不是成功。

 function FindContact(CompanyName,DivisionName,FirstName,LastName) {

        $.ajax({
            url: 'Path',
            dataType: "html or json",
            async:false,
            type:'post',
            data: {'FirstName':FirstName,'LastName':LastName,'DivisionName':DivisionName,'CompanyName':CompanyName},
            success: DisplayContacts,
            error: ErrorMsg
        });
    }

A quick to check if the json is valid is to just switch dataType to html and see the success is being fired. If it is than your json is invalid, if your still getting the same problem thers something else wrong.

快速检查 json 是否有效是将 dataType 切换为 html 并查看是否成功。如果它比您的 json 无效,如果您仍然遇到相同的问题,那么还有其他问题。

Another way to check if valid json is being returned is, open up firebug and when the request gets sent, click on the response tab, copy the response and got to jsonlint.com to check if its valid.

检查是否返回有效 json 的另一种方法是,打开 firebug,当请求发送时,单击响应选项卡,复制响应并访问 jsonlint.com 以检查其是否有效。

Jquery ajax docs

Jquery ajax 文档

回答by charltoons

My solution was a difference in Accept-Type and Content-Type headers. If the client-side request's Accept-Type header does not match the server-side response's Content-Type header, Jquery will call the error callback.

我的解决方案是 Accept-Type 和 Content-Type 标头的不同。如果客户端请求的 Accept-Type 头与服务器端响应的 Content-Type 头不匹配,Jquery 将调用错误回调。

Jquery, by default, sets the Accept-Type header to application/json. However, my server was sending a response with Content-Type application/x-javascript. My fix was to change the server-side response Content-Type header to the appropriate application/json.

默认情况下,Jquery 将 Accept-Type 标头设置为application/json. 但是,我的服务器正在发送带有 Content-Type 的响应application/x-javascript。我的解决方法是将服务器端响应 Content-Type 标头更改为适当的application/json.

回答by Kulbhushan Chaskar

I was facing the same issue when making an ajax call using jquery ajax. I fixed it using the following code:

使用 jquery ajax 进行 ajax 调用时,我遇到了同样的问题。我使用以下代码修复了它:

$.ajax({
  url: "/url",
  dataType: 'text',
  success: function(result){ alert(result); },
  error: ErrorMsg,
});

Note: dataType: 'text'parameter, it worked for me.

注意:dataType: 'text'参数,它对我有用。