Reputation: 371
I am trying to call api through Jquery ajax.Here is my simple jquery code
$(document).ready(function () {
jQuery.support.cors = true;
$.ajax({
url: 'http://[MyServername]/api/tracking/GetLocation?DeviceId=352136061098261',
type: 'GET',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert('success');
},
error: function (error) {
alert('error');
}
});
It directly moves to the error function and returns [object Object] error.
Upvotes: 0
Views: 1570
Reputation: 39025
To discover what's going on, instead of using a function that receives a single aprameter, use one with this signature:
Function( jqXHR jqXHR, String textStatus, String errorThrown )
Look for error in jquery ajax docs.
This will give you a more information.
However, much better than this, use your browser debugging tool: press F12 to access it. If it's Firefox you shpuld install Firebug. This tool allows you to check the network traffic (i.e.which request was sent, and which response was obtained).
Besides, if you use console.log(jqXHR, textStatus, errorThrown)
and open the consolse (pressing F12) you'll see the returned data more clearly. If you simply use alert()
you cannot see the details, and it's a much more obtrusive way of debugging (not recommended: it was used when there was no other remedy!!).
Another good way to debug is to use a REST debugging tool like Postman REST client for Chrome, that allows you to interact with a Web API using different methods (POST, PUT, GET, DELETE...) and sending and receiving information in different ways (JSON, form encoded, XML).
You can also use Fiddler, a free debugging tool that can be attached to a browser to examine the requests and responses.
NOTE: All of this are powerful debuggint tools, but if you want to get more concrete information, please, add the controller's action code to your question: there could be problems with routing, parameter binding, exceptions thrown by the action code, etc. Does your ajax call hit a breakpoint in the action code using VS debugger?
Upvotes: 1