admin管理员组文章数量:1424887
I am able to make a Ajax request and getting response as well in form of JSON string, still alert box of JavaScript is not showing actual response text. I am new to ajax concept and dont know much.
Ajax-Call:-
Action triggered on dropdown
<select name="state" onchange="getOptions(this.value)">
Javascript Function called :-
function getOptions(state){
AJAX.onreadystatechange = handler;
AJAX.open("GET", "getData?id="+state);
AJAX.send();
};
Response Firebug is showing
This is my code to fetch response and print.
function handler() {
if(AJAX.readyState == 4 && AJAX.status == 200) {
var json = eval('(' + AJAX.responseText +')');
alert('Success. Result:' + json);
}
else if (AJAX.readyState == 4 && AJAX.status != 200) {
alert('Something went wrong...');
}
}
Every time its a success but i get output as
I am able to make a Ajax request and getting response as well in form of JSON string, still alert box of JavaScript is not showing actual response text. I am new to ajax concept and dont know much.
Ajax-Call:-
Action triggered on dropdown
<select name="state" onchange="getOptions(this.value)">
Javascript Function called :-
function getOptions(state){
AJAX.onreadystatechange = handler;
AJAX.open("GET", "getData?id="+state);
AJAX.send();
};
Response Firebug is showing
This is my code to fetch response and print.
function handler() {
if(AJAX.readyState == 4 && AJAX.status == 200) {
var json = eval('(' + AJAX.responseText +')');
alert('Success. Result:' + json);
}
else if (AJAX.readyState == 4 && AJAX.status != 200) {
alert('Something went wrong...');
}
}
Every time its a success but i get output as
Share Improve this question edited Feb 19, 2015 at 8:54 dakab 5,92510 gold badges46 silver badges70 bronze badges asked Feb 19, 2015 at 7:47 user2332505user2332505 6492 gold badges10 silver badges22 bronze badges 4-
do
var res=JSON.parse(AJAX.responseText); alert(res.name)
...It will alertDehradun
– Rakesh_Kumar Commented Feb 19, 2015 at 7:48 - i tried that also , still same result – user2332505 Commented Feb 19, 2015 at 7:49
- 1 It is an array. You need res[0]. But why not use jQuery if you tag the question jQuery? – mplungjan Commented Feb 19, 2015 at 7:51
-
1
Do something like
res[0].name
.. whereres[0]
is the first object in array got afterJSON.parse(AJAX.responseText)
– Rakesh_Kumar Commented Feb 19, 2015 at 7:52
2 Answers
Reset to default 1You need to treat your response as JSON not as text.
Try this:
function handler() {
if (AJAX.readyState == 4 && AJAX.status == 200) {
var json = JSON.parse(AJAX.responseText), len = json.length, i = 0, txt = "";
for (; i < len; i++) {
if (txt) {
txt += ", ";
}
txt += json[i].name;
}
alert('Success. Result:' + txt);
} else if (AJAX.readyState == 4 && AJAX.status != 200) {
alert('Something went wrong...');
}
}
If you need to see the value as part of debugging the code, you should use
console.log(AJAX)
to inspect the value.
If you really need to display some message to the user, then reconsider showing the json result unformatted/filtered - that said you could iterate through all the objects properties and concenate theese in a string (like @Hiral shows).
本文标签: ajaxHow to print JSON response in javascriptStack Overflow
版权声明:本文标题:ajax - How to print JSON response in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745422414a2657959.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论