jQuery aspx error function always called, even with apparently valid return data
I am making an ajax call using jQuery (jquery-1.5.2.min.js). The server receives the call. fiddler shows the response text coming back apparently correct. But back in javascript my error: function is called instead of my success: function, and the data of the parameters to the error function don't give much clue as to the problem.
Here's the function where I make the initial call:
function SelectCBGAtClickedPoint()
{
$.ajax( {
type: "GET",
dataType: "text",
url: "http://localhost/ajax/SelectCBGAtPoint/1/2",
success:
function( msg )
{
alert( "success: " + msg );
},
error:
function( jqXHR, textStatus, errorThrown )
{
alert( "error: " + jqXHR + textStatus + errorThrown );
}
} );
}
Here's the function where I handle the call in my cherrypy server code:
def ajax( ajax, *args ):
with lock:
print "ajax request received: " + str( args )
cherrypy.response.headers[ "Content-Type" ] = "application/text"
return "{ 开发者_开发知识库x: 0 }"
Cherrypy is an odd beast, and I was thinking the problem must lie there. But as I say, I see both the query go out and the response come back in Fiddler. Here is what Fiddler shows as the raw view of the response:
HTTP/1.1 200 OK
Date: Mon, 11 Apr 2011 17:49:25 GMT
Content-Length: 8
Content-Type: application/text
Server: CherryPy/3.2.0
{ x: 0 }
Looks good, but then back in javascript, I get into the error: function, with the following values for the parameters (as shown in firebug):
errorThrown = ""
jqXHR = Object { readyState=0, status=0, statusText="error"}
statusText = "error"
I don't know where that word "error" is coming from. That string does not appear anywhere in my cherrypy server code.
Note that even though I'm returning a JSON string I've set the send and receive types to "text" for now, just to simplify in order to isolate the problem.
Any ideas why I'm getting this "error" reply, even when errorThrown is empty? Could it be that I haven't properly "initialized" either jQuery or jQuery.ajax?
My problem was the same as M Katz, I had my browser pointed to a file:///c:/temp.html and jquery was considering my request to be cross domain. However, setting the crossDomain property to true had no effect. I had to move the page under my web project, but once I did everything magically started to work.
精彩评论