如果readystate = 4且status = 200,则执行Jquery S.Ajax错误处理程序

时间:2011-05-01 23:45:47

标签: jquery ajax status readystate

我正在进行$ .ajax调用,它返回一个json响应,一切似乎都很好,但是调用成功处理程序,即使readystate = 4和状态,也会调用$ .ajax错误处理程序= 200。

$ .ajax调用是: -

    $.ajax({
        url: 'inc/ajax_printorder.asp',
        type: "GET",
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function (sendresponse) {
            var message = (typeof sendresponse.jsonresp) == 'string' ? eval('(' + sendresponse.jsonresp + ')') : sendresponse.jsonresp;
            if (message[0].ok == '1') {
                var order = window.open('', 'PrintWindow', 'width=600,height=600');
                var html = '<html><head><title>Print Your Order</title></head><body><div id="myprintorder">' + $('<div />').append(message[0].msg) + '</div></body></html>';
                order.document.open();
                order.document.write(html);
                order.document.close();
                return false;
            };
        },
        error: function (xhr, err) {
            alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
            alert("responseText: " + xhr.responseText);
        }
    });

来自Firebug,ajax响应是: -

{"jsonresp":[{"__type":"sendresponse","ok":"1","msg":"<h1>Your www.sandwichlunchesnewbury.co.uk order on 02/05/2011 00:34:01</h1><p>Website order from www.sandwichlunchesnewbury.co.uk on 02/05/2011 00:34:01 from:- </p><table width="60%" style="border:1px solid blue;padding:5px;"><tr><td>Name</td><td> a </td></tr><tr><td>Phone</td><td> b </td></tr><tr><td>Email</td><td>  </td></tr><tr><td>Business name</td><td>  </td></tr><tr><td>Delivery address</td><td> c </td></tr><tr><td>Date food required</td><td> Monday, 02/05/2011 </td></tr><tr><td>Time food required</td><td> 10 Am </td></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>Just Sandwiches (standard)</td><td>  </td></tr><tr><td>Just Sandwiches (gourmet)</td><td>  </td></tr><tr><td>Just Baguettes (standard)</td><td>  </td></tr><tr><td>Just Baguettes (gourmet)</td><td>  </td></tr><tr><td>Gourmet Bread Sandwiches</td><td> 2 </td></tr><tr><td>Sausage rolls</td><td>  </td></tr><tr><td>Cookie boxes</td><td>  </td></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>Total price</td><td> &pound;50.00 </td></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td colspan="2">Other info & special instructions </td></tr><tr><td colspan="2"> </td></tr></table>"}]}

有关为什么会出现错误而不是成功的想法?

由于

EPX

1 个答案:

答案 0 :(得分:9)

您的回复JSON无效,除其他外,您需要转义双引号(\"而不是")。您没有发布错误文本(传递给err的{​​{1}}的值)是什么,但我怀疑它是error

我发现JSONLint对于确保我收到/发送有效的JSON非常有用。