簡體   English   中英

JQuery AJAX,錯誤狀態代碼:200,狀態文本:parserorro | 好

[英]JQuery AJAX, Error Status code: 200, Status Text: parserorro | OK

這是我所處的一個有趣的情況。我正在使用VS 2008和.Net Framework 3.5開發一個ASP.Net網站,我想在測試頁面中使用jquery ajax,代碼如下所示:

C# Method
[WebMethod]
public static string test()
{
    return "Server Response" ;
}

$(document).ready(function() {
    $("#myDiv").click(function() {
        $.ajax({
            type: "POST",
            url: "AjaxTest.aspx/test",
            data: "",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
                        success: function(msg) {
                            // Replace the div's content with the page 
                            // method's return.
                            alert(msg.d);
                        },
                        error: function(result){ 
                            alert("error occured. Status:" + result.status  
                            + ' --Status Text:' + result.statusText 
                            + " --Error Result:" + result); 
                        }
           });
    });
});

所以當我像這樣使用Jquery 1.4.4時:

我明白了: Status 200; Status Text: OK Status 200; Status Text: OK

當我使用Jquery 1.5時,我得到: Status 200; Status Text: Parsererror Status 200; Status Text: Parsererror

所以我在Visual Studio中創建了一個新的WebSite,在那里復制和填充代碼,它工作得很好!!!! 我無法弄清楚導致問題的原因。 我也使用了帶參數的方法,並設置了數據: "{}" ,並完全刪除數據,但似乎沒有任何效果。

我不知道是否必須對我正在使用的DevExpress組件做任何事情。

我也找到了一個很好的答案,正在使用這樣的完整方法:

  complete: function(xhr, status) {
            if (status === 'error' || !xhr.responseText) {
                alert("Error");
            }
            else {
                var data = xhr.responseText;
                alert(data);
                //...
            }
        }

但我不知道它是否能正常工作,或者這種方法可能還有其他問題。 我也不知道如何從這里訪問響應數據。 但我主要擔心的是找出導致我網站問題的原因。

更新:今天在Google Chrome控制台中我注意到JQuery 1.5存在一些語法問題,如下所示:

未捕獲的SyntaxError:意外的標記<jQuery.jQuery.extend.globalEvaljquery.js:593 jQuery.ajaxSetup.converters.text scriptjquery.js:7175 ajaxConvertjquery.js:7074 donejquery.js:6622 jQuery.ajaxTransport.send.callbackjquery.js:7441

雖然這是一個很好的工具,但是這個問題並不是很容易用fiddler解決的。

我認為這里描述的問題,現在使用完整的事件。 有一些問題將在jQuery 1.5.1中解決。請參閱:

jQuery為ajax請求返回“parsererror”

因為它被張貼在那里,

complete: function (xhr, status) {
    if (status == 'error' || !xhr.responseText) {
        handleError();
    }
    else {
        var data = xhr.responseText;
        //...
    }
}

雖然有趣的是 - 當我查詢亞馬遜的服務時,這適用於我的jsonp數據(代碼亞馬遜基於網上的其他一些帖子我也沒有參考)ala:

//resp is simple a placeholder for autocomplete's response which I will need to call on a global scope.
        var resp;
        var filter;

        $(document).ready(function () {
            //http://completion.amazon.com/search/complete?method=completion&q=halo&search-alias=videogames&mkt=1&x=updateISSCompletion&noCacheIE=1295031912518
            filter = $("#productFilter").autocomplete({
                source: function (request, response) {
                    resp = response;

                    $.ajax({
                        url: "http://completion.amazon.com/search/complete",
                        type: "GET",
                        cache: false,
                        dataType: "jsonp",
                        success: function (data) {
                            //data[1] contains an array of the elements returned from the service.
                            //use .map to enumerate through them.
                            response($.map(data[1], function (item) {
                                //debugger;
                                 return { label: item, value: item, id: item}
                            }))

                        },
                        data: {
                            q: request.term,
                            "search-alias": "videogames",
                            mkt: "1",
                            callback: '?'
                        }
                    });
                },
                minLength: 2,
                select: function (event, ui) {
                    //$('#browseNode option:first').attr('selected', 'selected');
                    alert('selected');
                },
                open: function () {
                    $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
                },
                close: function () {
                    $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
                }
            });
        });
        //this is the method that will be called by the jsonp request
        function updateISSCompletion() {
            alert('updateiss');
            resp(completion[1]);
        }

您應該使用Fiddler - 一個出色的Web調試代理。 有了它的幫助,您可以監視服務器和客戶端之間的所有通信

不確定這是否有幫助,但是ajax()API指定他們已經更改了success()回調函數的返回對象。 這是來自jQuery API

從jQuery 1.5開始,成功回調函數接收一個“jqXHR”對象(在jQuery 1.4中,它接收到了XMLHttpRequest對象)。 但是,由於JSONP和跨域GET請求不使用XHR,因此在這些情況下,傳遞給成功回調的jqXHR和textStatus參數是未定義的。

你可以在這里找到它,如果它有幫助...

jQuery $ ajax API

我遇到了類似的問題,我無法從任何回調函數中提取JSON對象。

我想你可以在這個鏈接中找到這個問題的答案

我也有這個問題,但在PHP中我輸入'remote.php'

`echo $msg`' 

出現問題。 當我使用json_encode()

echo json_encode($msg);

一切正常。

這很奇怪,因為我從狀態為“OK”的服務器得到響應,因此函數“success”應該不是“錯誤”。 在'成功'我只有

success: function(res){ console.log(res);}

在我的情況下(當使用“jquery 1.9.1”時),添加dataType:“json”解決了“parsererror”問題(我之前沒有指定dataType並且發生了該問題)。

我遇到了類似的問題。

我用POST方法調用了AJAX REST服務並返回:

arguments [0] = status 200(OK)| arguments [1] =“parseerror”| arguments [2] =“無效的JSON:”

我的服務器方法返回“void”值。 為解決此問題,我將其替換為布爾值,例如。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM