0

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.

user3494471
  • 371
  • 1
  • 7
  • 21
  • 8
    Learn to use `console.log()` method, `alert()` is not a debugging tool. – Satpal Oct 16 '14 at 07:56
  • There is probably an error on the backend side because it goes to the error function. – rose000 Oct 16 '14 at 08:00
  • @rose I had checked url through fiddler and getting JSON response – user3494471 Oct 16 '14 at 08:05
  • [This Post](http://stackoverflow.com/questions/7852225/is-it-safe-to-use-support-cors-true-in-jquery) might be relevant to your question. Also, in the `error` function you forgot the double quotes in the `alert`. It should be `alert("error")`. It's better if you use `console.log` for debugging though, as pointed out by @Satpal. – Vivek Pradhan Oct 16 '14 at 08:21
  • change your error handler to `error: function (jqXHR, status, error) { alert(status + ':' + error); }` the first param is the jqXHR object that is why you are getting `[object Object]`... also as Other have pointed out use console logging... [Chrome Console](https://developers.google.com/chrome-developer-tools/docs/console), [Firefox](https://developer.mozilla.org/en-US/docs/Tools/Web_Console) – Arun P Johny Oct 16 '14 at 08:28

1 Answers1

1

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?

JotaBe
  • 38,030
  • 8
  • 98
  • 117