2015-05-23 129 views
1

我從我的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"}]" 

我不知道什麼是錯我的腳本。任何人都可以陪我?

+0

可以包含PHP的輸出你的JSON的片段? – diggersworld

+0

console.log(data);回報? – codegaze

+1

你的PHP JSON似乎無效。 – vinayakj

回答

2

您需要解析響應,JSON與JSON.parse方法,因爲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

+0

未捕獲的SyntaxError:意外的令牌s任何時候都是意外的令牌a或s,並且我確信沒有這樣的字符。 – Kubol

+0

多數民衆贊成在字符串(170)..從你的JSON ..喲需要有效的JSON字符串返回 – vinayakj

+0

@Kubol請確保您輸出有效的JSON字符串。在單獨的選項卡中打開'getclients/3'並檢查輸出是否爲[{「id」:「3」,「Name」:「Kontrahent#322」,「NIP」:「753」,「Adress」:「Wiosenna29」 「郵編」: 「20-201」, 「城市」: 「奧爾庫什」, 「電話」: 「12312312」, 「電子郵件」: 「[email protected]」, 「值」: 「0」}]'。 – dfsq

2

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

接着,

string(170) "[{"id":"3","Name":"Kontrahent#322","NIP":"753","Adress":"Wiosenna29","PostCode":"20-201","City":"Olkusz","Phone":"12312312","Email":"[email protected]","Value":"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":"[email protected]","Value":"0"}] 
+0

我有」返回json_encode($ data-> result());「 – Kubol

+0

看起來不錯,但你不是指'echo json_encode($ data-> result());'?如果這是函數的返回值,請確保稍後它是echo'd,而不是print_r'd – Drakes

+0

如果您接受了其他答案,您是如何更改PHP的? – Drakes

1

您的json數據不正確。

$result = array("id"=>"3","Name"=>"Kontrahent#322","NIP"=>"753","Adress"=>"Wiosenna29","PostCode"=>"20-201","City"=>"Olkusz","Phone"=>"12312312","Email"=>"[email protected]","Value"=>"0"); 

return json_encode($result); 

檢索JSON.parse方法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); 
    } 
} 
+0

這是對的,歡迎來到10分鐘前:) – Drakes