Jquery $.ajax callback not being called in ASP.NET MVC View Page
I have a simple ajax call:
$.ajax({url: my_url_here,
dataType: 'text',
success: function(data, textStatus) {
if(textStatus == "success") {
alert('success');
}
else {
alert('fail');
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
//do stuff
}
});
If I run this in a simple HTML file, I get the alert as expected. However if I run this in one of my ASP.NET MVC views, the ajax runs fine, but the callback function is never reached. I have tried creating a blank View page with nothing in it except jQuery and this script, to rule out any conflicts with my other Javascript.
I can see the request in Firebug, and it is returning a 200 response as expected, but it just doesn't reach the callback.
I have tried adding cache: false
paramater, played around with asynch
paramater, nothing seems to do the trick...
Any clues?
EDIT: Updated my jQuery with the error callback as suggested.
Error callback is reached in the View page, (not in the simple html test page however), and I get the following:
XMLHttpRequest.status is "0"
textStatus is "error"
errorThrown is "undefined"
EDIT 2 I should note that the URL I am requesting does not actually return anything, I am just trying to see if it exists- if I view the URL in my broswer it presents a simple text string. Am I using the wrong approach to this?
Why would it work fine in a simple HTML doc, but not within the ASP.NET MVC View? Does ASP do something to the ajax requests?
EDIT 3
So it turns out I was trying to access an external site, which is not allowed.
Seems odd that I was doing that fine from the simple test HTML file, and only ran into pr开发者_JAVA百科oblems when I was using the View page...
Implement the error
handler as well, just to be sure ..
error: function(XMLHttpRequest, textStatus, errorThrown) {...}
Update after your results
I believe the problem is that your links make a postback of the .net form..
try stopping the propagation of the event in your click handler (where you initiate the ajax call) something like
$('yourselector').click( function(e){
e.stopPropagation();
// now do your ajax call here ...
});
I get this quite alot and the error is pretty unhelpfull.
I would guess it has to be a problem with the url you are requesting.
精彩评论