jQuery 如何使用ajax响应在jquery中打印json数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19151930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to print json array in jquery with ajax response
提问by 404 Not Found
i am newbie in JSON. i just started learning JSON before 30 mins.
我是 JSON 新手。我刚开始在 30 分钟前学习 JSON。
i want to print JSON array in jquery.
我想在 jquery 中打印 JSON 数组。
$.ajax({
url : '/exp/resp.php',
type : 'POST',
dataType : 'json',
// data : 'uname='+uname+'&pass='+pass,
success : function (data)
{
alert(data);
}
});
now it's printing abc,def,ghi,jkl,mno. so i want to print it separate like abc def ghi etc.. i referred this answerbut it didn't help...
现在它正在打印abc,def,ghi,jkl,mno。所以我想像 abc def ghi 等一样单独打印它......我提到了这个答案,但它没有帮助......
回答by Daryl
Why not print something generic like:
为什么不打印一些通用的东西,比如:
JSON.stringify(data);
This should work with either an object or an array.
这应该适用于对象或数组。
回答by Arun P Johny
if data
is an array then
如果data
是一个数组然后
alert(data.join(' '));
回答by zzlalani
Since your data
is an array
Check the following tutorial
由于您data
是array
检查以下教程
Join the elements of an array into a string:
将数组元素连接成字符串:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.join();
The result of energy will be:
能量的结果将是:
Banana,Orange,Apple,Mango
Have a look at this http://www.w3schools.com/jsref/jsref_join.asp
看看这个http://www.w3schools.com/jsref/jsref_join.asp
For your code use Arun P Johny'ssolutions
对于您的代码,请使用Arun P Johny 的解决方案
alert(data.join(' '));