views:

503

answers:

2

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...

+2  A: 
Gaby
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
have a try with the updated answer..
Gaby
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
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
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 ?
Gaby
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
A: 

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