Get JSON object from AJAX call
Asked Answered
W

7

16

I'm new to AJAX and javascript. In my project, I have to get a json object in my javascript file. I've used spray-json and it shows me the json object in the url. http://localhost:8081/all-modules

{
  "status": "S1000",
  "description": "Success",
  "results": ["module1", "module2", "module3"]
}

My Ajax call

  $.ajax({
        url: 'http://localhost:8081/all-modules',
        dataType: 'application/json',
        complete: function(data){
            alert(data)
        },
        success: function(data){
            alert(data)
        }

It returns an alert [object Object]. What is the issue in here?

Wivestad answered 6/3, 2014 at 7:14 Comment(5)
Try alerting data.status. I don't think there's an issue. You just cant alert an objectBudding
alert will implicitly call toString on data, which will be an object literal (how the JSON is parsed), and that value is "[object Object]"Personalize
#1036528Adriatic
Use JSON.stringify(data) while displaying in alert as alert cannot display the object. It will convert object to string format.Seumas
That is already a JSON objectCarvalho
E
19

Try the following;

var data = '{"name": "John","age": 30}';

var json = JSON.parse(data);

alert(json["name"]);
alert(json.name);

You can also check out this link: How to access JSON object in JavaScript

Eggnog answered 12/5, 2015 at 12:51 Comment(0)
M
4

If you wish to see all the data in the JSON object, use JSON.stringify Refer here for more details

Hope that helps.

Morisco answered 6/3, 2014 at 7:20 Comment(0)
K
3

just console.log(data) you will see your object.

you can access your value by something like this

data.id //will give you id

it also depend on you json how you are creating check this out for explanation

// if it simply json then access it directly
//Example => {"id":1,"value":"APPLE"}
data.id; // will give you 1 

// if it json array then you need to iterate over array and then get value.
//Example => [{"id":1,"value":"APPLE"},{"id":2,"value":"MANGO"}] then
data[0].id;  // will give you 1 

so your code will be like this

 $.ajax({
    url: 'http://localhost:8081/all-modules',
    dataType: 'application/json',
    complete: function(data){
        alert(data.status);// S1000
        alert(data.description);// Success
        // for results you have to iterate because it is an array
        var len =  data.results.length;
        for(var i=0;i<len;i++ ){
            alert(data.results[i]);
        }
    },
    success: function(data){
        alert(data)
    }
})
Kerch answered 6/3, 2014 at 7:17 Comment(0)
C
0

try console.log() it will log on the console. alert doesnot displays the object.

 $.ajax({
    url: 'http://localhost:8081/all-modules',
    dataType: 'application/json',
    complete: function(data){
        console.log(data)
    },
    success: function(data){
        console.log(data)
    }
Chevy answered 6/3, 2014 at 7:17 Comment(0)
C
0

data is no longer in JSON format, it is a Javascript Object. You do not need to use function like jQuery.parseJSON anymore.

It is one common mistake for beginners.

If you want to see this Javascript Object, try alert(JSON.stringify(data));

Cort answered 6/3, 2014 at 7:23 Comment(0)
P
0

i think you just printing the object.Try someting like this

$.ajax({
    url: 'http://localhost:8081/all-modules',
    dataType: 'application/json',
    complete: function(data){
        alert("status = "+data.status+"descripttion"+data.description);
    },
    success: function(data){
         alert("status = "+data.status+"descripttion"+data.description);
    }
Patricepatrich answered 6/3, 2014 at 10:43 Comment(0)
D
0

Try data[0].status;. Your data are in an object now. At console.log(data) you can see that

Darsie answered 7/5, 2014 at 8:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.