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

2 Answers 2

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 ...
});
Sign up to request clarification or add additional context in comments.

5 Comments

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
This didn't solve it. Is it likely to be this: stackoverflow.com/questions/872206/…
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.
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 ?
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!
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.