我從我的PHP JSON數據以這種形式:jQuery的JSON返回undefined
string(170) "[{"id":"3","Name":"Kontrahent#322","NIP":"753","Adress":"Wiosenna29","PostCode":"20-201","City":"Olkusz","Phone":"12312312","Email":"[email protected]","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);或alert(data.Name);返回undefined。 console.log(data); 返回:
string(141) "[{"id":"1","Name":"Kontrahent #1","NIP":"735256985","Adress":"","PostCode":"","City":"","Phone":"777555888","Email":"[email protected]","Value":"0"}]"
我不知道什麼是錯我的腳本。任何人都可以陪我?
可以包含PHP的輸出你的JSON的片段? – diggersworld
console.log(data);回報? – codegaze
你的PHP JSON似乎無效。 – vinayakj