1

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 problems when I was using the View page...

elwyn
  • 10,360
  • 11
  • 42
  • 52

2 Answers2

2

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 ...
});
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • Updated OP- this is a good bit of advice! Error callback is now called in the View page, but not in the html test page – elwyn Feb 11 '10 at 21:54
  • This didn't solve it. Is it likely to be this: http://stackoverflow.com/questions/872206/http-status-code-0-what-does-this-mean-in-ms-xmlhttp – elwyn Feb 11 '10 at 22:18
  • The URL points at a plain text string, perhaps I am going about this the wrong way, perhaps I should try and figure out how to do a HEAD request, and check for a 404 error. EDIT: Doesn't look like I can do this to an external domain. Hmm, the plot thickens again. – elwyn Feb 11 '10 at 22:19
  • Are you trying with external domains ? you cannot do that.. You can only call pages on the same domain .. This is a security issue (*with existing workarounds..*).. Also are you running this on local network ? or on live urls ? – Gabriele Petrioli Feb 11 '10 at 22:41
  • Looks like this is the issue- I was trying to access an external domain. Running on localhost. I will try and accomplish this another way. Thanks for your help! – elwyn Feb 11 '10 at 22:48
0

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.

Rigobert Song
  • 2,766
  • 2
  • 30
  • 47