我是AJAX和javascript的新手。在我的项目中,我必须在javascript文件中获得一个json对象。我使用过spray-json,它显示了url中的json对象。http://localhost:8081/all-modules
{
"status": "S1000",
"description": "Success",
"results": ["module1", "module2", "module3"]
}我的Ajax调用
$.ajax({
url: 'http://localhost:8081/all-modules',
dataType: 'application/json',
complete: function(data){
alert(data)
},
success: function(data){
alert(data)
}它返回一个警报[object Object]。这里有什么问题?
发布于 2015-05-12 20:51:08
尝试下面的方法;
var data = '{"name": "John","age": 30}';
var json = JSON.parse(data);
alert(json["name"]);
alert(json.name);您还可以查看此链接:How to access JSON object in JavaScript
发布于 2014-03-06 15:20:45
如果您希望查看JSON对象中的所有数据,请使用JSON.stringify Refer here了解更多详细信息
希望这能有所帮助。
发布于 2014-03-06 15:17:44
只需console.log(数据),你就会看到你的对象。
您可以通过如下方式访问您的值
data.id //will give you id它还取决于您是如何创建的。请查看这一点以获得解释
// 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 因此,您的代码将如下所示
$.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)
}
})https://stackoverflow.com/questions/22217635
复制相似问题