jQuery JSON returning undefined
jQuery JSON returning undefined
我有数据从我的php json在这个形式:
string(170) "[{"id":"3","Name":"Kontrahent#322","NIP":"753","Adress":"Wiosenna29","PostCode":"20-201","City":"Olkusz","Phone":"12312312","Email":"jakub@edu.pl","Value":"0"}]"
和我的功能:
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var val = $('#test').val()
var id = $('#clientsname option').filter(function() {
return this.value == val;
}).data('id');
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
var data = xmlhttp.responseText;
alert(data[0].Name);
}
}
xmlhttp.open("GET","getclients/"+id);
xmlhttp.send();
}
}
警报(数据[0]. name);或警报(data.Name);返回未定义。console.log(数据);返回:
string(141) "[{"id":"1","Name":"Kontrahent #1","NIP":"735256985","Adress":"","PostCode":"","City":"","Phone":"777555888","Email":"e@mail.pl","Value":"0"}]"
我不知道我的剧本出了什么问题。有人能帮我吗?
您需要使用JSON.parse
方法将响应解析为JSON,因为xmlhttp.responseText
只是一个字符串:
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
var data = JSON.parse(xmlhttp.responseText);
alert(data[0].Name);
}
}
演示:http://plnkr.co/edit/LygRQEu89LnQXW6TWDMa?p=preview
xmlhttp.responseText
返回文本。如果要解析JSON,请使用JSON.parse(xmlhttp.responseText)
。因此
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
// var data = xmlhttp.responseText;
var data = JSON.parse(xmlhttp.responseText);
alert(data[0].Name);
}
}
Uncaught SyntaxError: Unexpected token s
,
<>之前的字符串(170)"[{" id ":"3","名字":"Kontrahent # 322","捏":"753","地址":"Wiosenna29"、"邮政编码":"20 - 201","城市":"Olkusz","电话":"12312312","电子邮件":"jakub@edu.pl"、"价值":"0"}]"之前不是JSON。这看起来像PHP中的print_r
。如果您有一个有效的JSON字符串,请使用echo
,例如在PHP中使用json_encode()
。有效的JSON应该是这样的:
[{"id":"3","Name":"Kontrahent#322","NIP":"753","Adress":"Wiosenna29","PostCode":"20-201","City":"Olkusz","Phone":"12312312","Email":"jakub@edu.pl","Value":"0"}]
您的json数据不正确。
$result = array("id"=>"3","Name"=>"Kontrahent#322","NIP"=>"753","Adress"=>"Wiosenna29","PostCode"=>"20-201","City"=>"Olkusz","Phone"=>"12312312","Email"=>"jakub@edu.pl","Value"=>"0");
return json_encode($result);
从json检索json数据。解析方法
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
var data = JSON.parse(xmlhttp.responseText);
alert(data[0].Name);
}
}
相关文章:
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- /undefined在我的404错误日志中多次出现
- RequireJS向模块传递配置总是返回undefined
- noty.js is returning undefined
- User getSessionToken() Returning Undefined
- JQuery val returning undefined
- jqGrid returning rowObject undefined
- Collection.get returning undefined in backbone.js
- PhoneGap getDirectory() returning undefined
- getElementById is returning undefined
- jquery .attr() returning as undefined
- Javascript returning undefined
- obj and $this returning undefined
- json_encode is returning undefined from Javascript
- openui5 getBinding() returning undefined
- $resource angular returning undefined with $promise
- Document.getElementById.value returning Undefined
- Ajax post returning undefined
- Javascript .each() returning 'undefined'
- jQuery JSON returning undefined