简体   繁体   English

无法在页面上显示JSON

[英]Can't display JSON on page

I have been trying to get my JSON code from a PHP file which is connected to my database to display on my website using an XMLHttpRequest , but we cant split them and display it. 我一直在尝试从连接到我的数据库的PHP文件中获取我的JSON代码,以使用XMLHttpRequest在我的网站上显示,但我们无法拆分它们并显示它。

We are using an php file example.php that does the following: 我们正在使用php文件example.php执行以下操作:

function jsonFromQuery($result) {
    if (mysql_num_rows($result) > 0) {
        while($r = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $json[] = $r;
        }
    } else {
        $json = "Table is empty";
    }

    return json_encode($json);
}

This will display the json code as following on the website if you open the php file: 如果你打开php文件,这将在网站上显示如下的json代码:

[{"UserID":"2","Firstname":"Piet","Lastname":"Klaas","Age":"23","Specialization":"Voet","Branch":"Been","Interests":"Hoofd","Hospital":"OLVG","Email":"pietklaas@gmail.com","ProfilePicture":""}]

With this we use the following javascript file to request this php file and stringify and try and display it. 有了这个,我们使用以下javascript文件来请求这个php文件和stringify并尝试显示它。

var request = new XMLHttpRequest;
request.open('GET' , 'http://myurl.nl/example.php', false);
request.send(null);

if (request.status == 0)
    console.log(request.responseText);

var obj = JSON.stringify(request);
var firstname = obj.Firstname;
document.writeln(firstname);`

But I get a massive string which includes the type status etc. And don't know how to split this up to display on their own. 但是我得到了一个包含类型状态等的大量字符串。并且不知道如何将其拆分为自己显示。 For example only Firstname = Piet on the page. 例如,页面上只有Firstname = Piet。

When you get the data back from PHP it's already in the form of a string. 当您从PHP获取数据时,它已经是字符串形式。 You need to use JSON.parse() to convert it into an object you can use. 您需要使用JSON.parse()将其转换为可以使用的对象。 Try... 尝试...

var obj = JSON.parse(request.responseText);
var firstname = obj[0].Firstname;
document.writeln(firstname);`

Note as well that the Json you're getting back is a list of objects: [{...},{...},{...}] , so you can't just call .Firstname as you haven't specified which object you care about. 还要注意,你要回来的Json是一个对象列表: [{...},{...},{...}] ,所以你不能只是调用.Firstname t指定您关心的对象。 Hence the [0] in the example above to pick out the first object. 因此,上面例子中的[0]挑出第一个对象。

One other thought... At present if your PHP doesn't find any results it's going to send back something that isn't in the format you're expecting. 另一个想法......目前,如果你的PHP没有找到任何结果,它将发回一些不符合你期望的格式的东西。 Consider either wrapping the list in an object with a status... 考虑将列表包装在具有状态的对象中......

{
    "Success": true,
    "Results": [{...}, {...}]
}

Or make the PHP script return a different HTTP code to indicate failure (404 seems appropriate here) 或者让PHP脚本返回一个不同的HTTP代码来指示失败(这里404似乎合适)

For future reference, JSON.stringify does the opposite - it takes a complex object and converts it into Json 为了将来的参考,JSON.stringify正好相反 - 它需要一个复杂的对象并将其转换 Json

You are parsing the whole XMLHttpRequest object 您正在解析整个XMLHttpRequest对象

var obj = JSON.parse(request);

try using just the response body 尝试只使用响应体

var obj = JSON.parse(request.responseText);

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

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