0

I am making an AJAX request to my GPS server, which returns the following JSON format in case of success,

{"result":[{"longitude":76.391529,"latitude":27.974347,"location":"Delhi-Ajmer Expressway - Madhosinghpura- Alwar - Rajasthan - India","speed":0,"dttime":"14 Feb 2016 00:38:47","ignition":0,"vehicle_name":"ABCD","icon":0}]}

and in case of failure :

{"result":[{"error" : "Vehicle location did not found" }]}

This is my code :

ajaxURL = "http://www.vehicletrack.biz/api/vehlastlocation?token=K2ZFKFMP3A&vehname="+vehicleNum;

//start ajax request
$.ajax({
    url: ajaxURL,
    dataType: "text",
    success: function(data) {
        alert(data[0].longitude);
    }
});

Firstly, I want to handle the error case and if result is proper then I want to display each field. Can anyone please help me with accessing the elements of JSON response and handle the error case.

PS : This is my first time with JSON and googling didn'r return relevant JSON format example. Hit and Trial didn't help.

3
  • Based on what you've posted, wouldn't it be data.result[0].longitude Commented Feb 13, 2016 at 19:27
  • Don't you need a $.parseJSON(data); inside your success handler? Commented Feb 13, 2016 at 19:28
  • 1
    To simplify your work open chrome developer tools and in console put: var result = {"result":[{"error" : "Vehicle location did not found" }]} then press enter then put result and press enter again Commented Feb 13, 2016 at 19:29

3 Answers 3

1

You can handle error case like following.

success: function(data) {
    data = JSON.parse(data);
    if(data.result[0].error){
        // error
        alert('Error'); 
    } else {
        // access all properties using loop

        var obj = data.result[0];
        for (var prop in obj ) {
            var propValue = obj[prop];  // access property like this
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I still get data.result is undefined.
Use JSON.parse. You can also try dataType: "json". See updated answer. @InsaneCoder
0

Change dataType to json

//start ajax request
$.ajax({
    url: ajaxURL,
    dataType: "json",
    success: function(data) {
        var json = data.result[0].longitude;
        alert(json);
    }, error: function(data) {
        var err = data.result[0].error;
        alert(err);
    }
});

Comments

0

you are trying to return your result data as json, and treating your result as an object. However, the datatype specified in your ajax call is text. I have never used 'text' as a dataType, but I am pretty sure that won't work. Try dataType:'json' instead if you want to process / parse the result into an object from json.

given that, you would want something like:

    //start ajax request
    $.ajax({
        url: ajaxURL,
        dataType: "json",
        success: function(data) {
          var str = 'result ';
          var a = data.result;
          for (var i = 0; i < data.result.length; i++)
          {
          foreach (prop in item)
          {
             // do stuff
             str += ' ' + prop;

          }
          }
          alert( str );
        }
    });

One problem you can run into here is using a cross domain requests (the domain you are sending in your ajax call is not the same as the one your web page is located). If that is the case, you have to do some special handling on the server and use the jsonp data type. You can get details here .

Hope this is what you are looking for.

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.