从AJAX调用获取JSON对象

时间:2021-07-24 01:23:56

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

我对AJAX和javascript不熟悉。在我的项目中,我必须在javascript文件中获得一个json对象。我使用了spray-json,它显示了url中的json对象。http://localhost:8081 /所有的模块

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

My Ajax call

我的Ajax调用

  $.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?

它返回一个alert [object]。这里的问题是什么?

7 个解决方案

#1


14  

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

您还可以查看这个链接:如何在JavaScript中访问JSON对象

#2


4  

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

如果希望查看JSON对象中的所有数据,请使用JSON。stringify在这里参考更多细节

Hope that helps.

希望有帮助。

#3


1  

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

只需console.log(data)就可以看到对象。

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

它还取决于您如何创建json,请检查一下,以便进行解释

// 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)
    }
})

#4


0  

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

尝试console.log(),它会登录到控制台。alert不会显示对象。

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

#5


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);
    }

#6


0  

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

数据不再是JSON格式,而是一个Javascript对象。您不需要使用jQuery这样的函数。parseJSON了。

It is one common mistake for beginners.

这是初学者常犯的一个错误。

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

如果您想查看此Javascript对象,请尝试alert(JSON.stringify(data));

#7


0  

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

试数据[0].status;。你的数据现在在一个对象中。在console.log(data)中可以看到这一点

#1


14  

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

您还可以查看这个链接:如何在JavaScript中访问JSON对象

#2


4  

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

如果希望查看JSON对象中的所有数据,请使用JSON。stringify在这里参考更多细节

Hope that helps.

希望有帮助。

#3


1  

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

只需console.log(data)就可以看到对象。

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

它还取决于您如何创建json,请检查一下,以便进行解释

// 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)
    }
})

#4


0  

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

尝试console.log(),它会登录到控制台。alert不会显示对象。

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

#5


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);
    }

#6


0  

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

数据不再是JSON格式,而是一个Javascript对象。您不需要使用jQuery这样的函数。parseJSON了。

It is one common mistake for beginners.

这是初学者常犯的一个错误。

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

如果您想查看此Javascript对象,请尝试alert(JSON.stringify(data));

#7


0  

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

试数据[0].status;。你的数据现在在一个对象中。在console.log(data)中可以看到这一点