[英]JQuery AJAX, Error Status code: 200, Status Text: parserorro | OK
Here is a funny situation that I'm in. I'm developing an ASP.Net web site using VS 2008 and .Net Framework 3.5, and I want to use jquery ajax in a test page, the code looks like this: 这是我所处的一个有趣的情况。我正在使用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);
}
});
});
});
So When I use Jquery 1.4.4 like this : 所以当我像这样使用Jquery 1.4.4时:
I get : Status 200; Status Text: OK
我明白了:
Status 200; Status Text: OK
Status 200; Status Text: OK
When I use Jquery 1.5 I get: Status 200; Status Text: Parsererror
当我使用Jquery 1.5时,我得到:
Status 200; Status Text: Parsererror
Status 200; Status Text: Parsererror
So I created a new WebSite in Visual Studio, copy and pased the code there, and it works fine !!!! 所以我在Visual Studio中创建了一个新的WebSite,在那里复制和填充代码,它工作得很好!!!! I can't figure out what causes the problem.
我无法弄清楚导致问题的原因。 Also I have used methods with parameter, and setting data:
"{}"
, and removing data completely, but nothing seems to work. 我也使用了带参数的方法,并设置了数据:
"{}"
,并完全删除数据,但似乎没有任何效果。
I don't know if has to do anything with the DevExpress components that I'm using or not. 我不知道是否必须对我正在使用的DevExpress组件做任何事情。
I also found a good answer which was working with complete method like this : 我也找到了一个很好的答案,正在使用这样的完整方法:
complete: function(xhr, status) {
if (status === 'error' || !xhr.responseText) {
alert("Error");
}
else {
var data = xhr.responseText;
alert(data);
//...
}
}
But I don't know if it will work fine or there might be some other problem with this method too. 但我不知道它是否能正常工作,或者这种方法可能还有其他问题。 I also don't know how to access response data from here.
我也不知道如何从这里访问响应数据。 But my main concern is finding out what is causing the problem in my website.
但我主要担心的是找出导致我网站问题的原因。
UPDATE: Well today in Google Chrome console I noticed some syntax problems with JQuery 1.5 they are as below: 更新:今天在Google Chrome控制台中我注意到JQuery 1.5存在一些语法问题,如下所示:
Uncaught SyntaxError: Unexpected token < 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 未捕获的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
The issue isn't so easily solved with fiddler, although it's a great tool. 虽然这是一个很好的工具,但是这个问题并不是很容易用fiddler解决的。
The issue I think is described here, and for now use the complete event. 我认为这里描述的问题,现在使用完整的事件。 there are some issues that will be resolved in jQuery 1.5.1 See:
有一些问题将在jQuery 1.5.1中解决。请参阅:
jQuery returning "parsererror" for ajax request jQuery为ajax请求返回“parsererror”
as it was posted there, 因为它被张贴在那里,
complete: function (xhr, status) { if (status == 'error' || !xhr.responseText) { handleError(); } else { var data = xhr.responseText; //... } }
Although the interesting thing is - this works for me with jsonp data when I query amazon's service (code amazon was based on some other posting on the net I don't have the ref too) ala: 虽然有趣的是 - 当我查询亚马逊的服务时,这适用于我的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]); }
Not sure if this will help, but the ajax() API specifies that they have changed the return object for the success() callback function. 不确定这是否有帮助,但是ajax()API指定他们已经更改了success()回调函数的返回对象。 This is from the jQuery API
这是来自jQuery API
As of jQuery 1.5, the success callback function receives a "jqXHR" object (in jQuery 1.4, it received the XMLHttpRequest object).
从jQuery 1.5开始,成功回调函数接收一个“jqXHR”对象(在jQuery 1.4中,它接收到了XMLHttpRequest对象)。 However, since JSONP and cross-domain GET requests do not use XHR, in those cases the jqXHR and textStatus parameters passed to the success callback are undefined.
但是,由于JSONP和跨域GET请求不使用XHR,因此在这些情况下,传递给成功回调的jqXHR和textStatus参数是未定义的。
You can find it here if it helps at all... 你可以在这里找到它,如果它有帮助...
jQuery $ajax API jQuery $ ajax API
I am running into a similar problem, and am unable to pull the JSON object from any callback functions. 我遇到了类似的问题,我无法从任何回调函数中提取JSON对象。
我想你可以在这个链接中找到这个问题的答案
I had this problem too but in PHP When i put in 'remote.php'
: 我也有这个问题,但在PHP中我输入
'remote.php'
:
`echo $msg`'
problem occurs. 出现问题。 When I use
json_encode()
: 当我使用
json_encode()
:
echo json_encode($msg);
then everything works. 一切正常。
This is strange, because I get response from server with status 'OK', so then function 'success' should work not 'error'. 这很奇怪,因为我从状态为“OK”的服务器得到响应,因此函数“success”应该不是“错误”。 In 'success' i have only
在'成功'我只有
success: function(res){ console.log(res);}
在我的情况下(当使用“jquery 1.9.1”时),添加dataType:“json”解决了“parsererror”问题(我之前没有指定dataType并且发生了该问题)。
I had a similar problem. 我遇到了类似的问题。
I called in AJAX a REST service with POST method and got back : 我用POST方法调用了AJAX REST服务并返回:
arguments[0] = status 200 (OK) | arguments [0] = status 200(OK)| arguments[1] = "parseerror" |
arguments [1] =“parseerror”| arguments[2] = "Invalid JSON :"
arguments [2] =“无效的JSON:”
My server method returned a "void" value. 我的服务器方法返回“void”值。 To resolve the problem, I replaced it by a Boolean value for example.
为解决此问题,我将其替换为布尔值,例如。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.