gpt4 book ai didi

c# - JQuery AJAX,错误状态代码 : 200, 状态文本:parserorro |好的

转载 作者:太空狗 更新时间:2023-10-29 23:03:08 25 4
gpt4 key购买 nike

这是我所处的一个有趣的情况。我正在使用 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 时:

我得到:状态 200;状态文本:OK

当我使用 Jquery 1.5 时,我得到:Status 200;状态文本:Parsererror

所以我在 Visual Studio 中创建了一个新网站,将代码复制并粘贴到那里,它工作正常!!!!我不知道是什么原因导致了这个问题。我也使用了带参数的方法,并设置了数据:"{}",并完全删除了数据,但似乎没有任何效果。

我不知道是否必须对我正在使用的 DevExpress 组件做任何事情。

我还找到了一个很好的答案,它正在使用这样的完整方法:

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

但我不知道它是否可以正常工作,或者这种方法也可能存在其他问题。我也不知道如何从这里访问响应数据。但我主要关心的是找出导致我的网站出现问题的原因。

更新:今天在 Google Chrome 控制台中,我注意到 JQuery 1.5 存在一些语法问题它们如下:

未捕获的语法错误:意外的标记

最佳答案

虽然 fiddler 是一个很棒的工具,但这个问题并没有那么容易解决。

我认为的问题描述在这里,现在使用完整的事件。有一些问题将在 jQuery 1.5.1 中解决见:

jQuery returning "parsererror" for ajax request

因为它被张贴在那里,

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

关于c# - JQuery AJAX,错误状态代码 : 200, 状态文本:parserorro |好的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5540905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com