Chris
Chris

Reputation: 298

Why does this AJAX request always fail?

I'm new to both Ajax and Javascript, and I'm having a heck of a time figuring out why this request is always failing:

function sellStock(sellData){   
$.ajax({             
    url: '../controller/sell.php',      
    data: sellData,
    success: function(){
        alert("MADE IT");
        //does stuff                            
    }                               
});
}

The only data I can get back about the failed request is "error." What's the best way to get more info about the problem?

I'm especially confused because this almost-identical function works perfectly on a php file from the same directory:

function autoUpdate(s){
$.ajax({ 
    url: '../controller/quote.php',
    data: {symbol: s}, 
    success: function(data){
        //does stuff             
    }                       
});
}

There are slight differences between the calls (i.e. autoUpdate() is called from setInterval() while sellStock() is called in a loop by another JS function) but I can't see that any would be a problem. I'm confident that sellData is in the right form. I've accessed each member that should be there, and, just in case, I've tried it with an anonymous object with the same result.

Can anyone lend me a hand?

Upvotes: 1

Views: 382

Answers (1)

Alwyn
Alwyn

Reputation: 8337

  1. Download fiddler and run a trace, if you're using IE9+, hit F12, and push the "Start Capturing" button, under the Network tab.
  2. If you see a request there, great, dbl click to see the details and may be you'll find your error message there.
  3. Alternatively look up jQuery Error handler in the ajax call, the parameters will get you more detail on your request.
  4. If none of the above works, go back to fiddler, click on the composer tab try to hand-serialize the payload sellData and keep debugging.

Something above should give you a clue to what went wrong.

Upvotes: 4

Related Questions