简体   繁体   English

jQuery JSON返回未定义

[英]jQuery JSON returning undefined

I have data from my php json in this form: 我以这种形式从我的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"}]"

and my function: 和我的功能:

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();
    }
}  

alert(data[0].Name); alert(data [0] .Name); or alert(data.Name); 或alert(data.Name); returning undefined. 返回未定义。 console.log(data); console.log(data); return: 返回:

string(141) "[{"id":"1","Name":"Kontrahent #1","NIP":"735256985","Adress":"","PostCode":"","City":"","Phone":"777555888","Email":"e@mail.pl","Value":"0"}]"

I don't know what is wrong with my script. 我不知道我的脚本出了什么问题。 Anyone can hellp me? 有人可以欺负我吗?

You need to parse response as JSON with JSON.parse method, because xmlhttp.responseText is just a string: 您需要使用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);
    }
}

Demo: http://plnkr.co/edit/LygRQEu89LnQXW6TWDMa?p=preview 演示: http //plnkr.co/edit/LygRQEu89LnQXW6TWDMa?p = preview

xmlhttp.responseText returns text. xmlhttp.responseText返回文本。 If you want to parse the JSON, use JSON.parse(xmlhttp.responseText) . 如果要解析JSON,请使用JSON.parse(xmlhttp.responseText) Thus 从而

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 未捕获到的SyntaxError:意外令牌

Next, 下一个,

string(170) "[{"id":"3","Name":"Kontrahent#322","NIP":"753","Adress":"Wiosenna29","PostCode":"20-201","City":"Olkusz","Phone":"12312312","Email":"jakub@edu.pl","Value":"0"}]"

is not JSON. 不是JSON。 This looks like print_r from PHP. 看起来像PHP中的print_r Use echo instead if you have a valid JSON string, say by using json_encode() in PHP. 如果您有有效的JSON字符串,请使用echo ,例如在PHP中使用json_encode() Valid JSON will look like this: 有效的JSON如下所示:

[{"id":"3","Name":"Kontrahent#322","NIP":"753","Adress":"Wiosenna29","PostCode":"20-201","City":"Olkusz","Phone":"12312312","Email":"jakub@edu.pl","Value":"0"}]

Your json data is not correct. 您的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);

Retrieve json data from JSON.parse method 从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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM