ajax jquery始终运行错误 [英] ajax jquery always running Error

查看:92
本文介绍了ajax jquery始终运行错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我运行ajax jquery函数时,都会出现错误,这适用于我所有的ajax调用. 这是我的代码示例

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

 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);
        }
    }

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

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

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

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

请帮助!

推荐答案

确保您返回的内容是有效的json.如果不正确,并且服务器上的所有操作都正确,则$ .ajax将出错而不是成功.

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.


 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
        });
    }

快速检查json是否有效的方法是将dataType切换为html,然后查看是否成功.如果不是,那么json是无效的,如果仍然遇到相同的问题,那还会有其他问题.

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的方法是,打开firebug,然后在发送请求时,单击响应"选项卡,复制响应并到达jsonlint.com,以检查其是否有效.

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.

Jquery ajax文档

这篇关于ajax jquery始终运行错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆