Javascript 如何将 JSON 从 PHP 获取到 JS?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11516962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get JSON from PHP to JS?
提问by Tom
I have really been searching for almost 2 hours and have yet to find a good example on how to pass JSON data from PHP to JS. I have a JSON encoding script in PHP that echoes out a JSON script that looks more or less like this (pseudocode).
我真的已经搜索了将近 2 个小时,但还没有找到一个关于如何将 JSON 数据从 PHP 传递到 JS 的好例子。我在 PHP 中有一个 JSON 编码脚本,它呼出一个看起来或多或少像这样(伪代码)的 JSON 脚本。
{
"1": [
{"id":"2","type":"1","description":"Foo","options:[
{"opt_id":"1","opt_desc":"Bar"},
{"opt_id":"2","opt_desc":"Lorem"}],
{"id":"3","type":"3","description":"Ipsum","options:[
...
"6":
{"id":"14","type":"1","description":"Test","options:[
...
etc
Problem is, how can I get this data with JavaScript? My goal is to make a .js script that generates a poll based on these JSON datas, but I honest to god can't find any examples on how to do this. Guessing it is some something like:
问题是,如何使用 JavaScript 获取这些数据?我的目标是制作一个基于这些 JSON 数据生成投票的 .js 脚本,但老实说,我找不到任何关于如何执行此操作的示例。猜测它是这样的:
Obj jsonData = new Object();
jsonData = $.getJson('url',data,function()){
enter code here
}
Any links to any good examples or similar would be highly appreciated. And I thought that encoding the data in PHP was the tricky part...
任何好的例子或类似的任何链接将不胜感激。我认为用 PHP 编码数据是棘手的部分......
EDIT:
编辑:
I got this code snippet to work, so I can review my whole JSON data in JS. But now I can't seem to figure out how to get to the inner data. It does print out the stage number (1-6) but I can't figure out how to get the question data, and then again the options data within each question. Do I have to experiment with nested each loops?
我让这个代码片段起作用,所以我可以在 JS 中查看我的整个 JSON 数据。但现在我似乎无法弄清楚如何获取内部数据。它确实打印出阶段编号(1-6),但我无法弄清楚如何获取问题数据,然后再次获取每个问题中的选项数据。我是否必须尝试嵌套每个循环?
$(document).ready(function()
{
$('#show-results').click(function()
{
$.post('JSAAN.php', function(data)
{
var pushedData = jQuery.parseJSON(data);
$.each(pushedData, function(i, serverData)
{
alert(i);
})
})
})
});
The idea here is to get into the question information in the middle level and print out the qusetion description, then based on the question type - loop through the options (if any) to create checkbox/radiobutton-groups before going on to the next question. The first number represents which stage of the multi stage poll I am currently working on. My plan is to divide it into 6 stages by hiding/showing various divs until the last page where the form is submitted through Ajax.
这里的想法是进入中间级别的问题信息并打印出问题描述,然后根据问题类型 - 在继续下一个问题之前循环浏览选项(如果有)以创建复选框/单选按钮组. 第一个数字代表我目前正在进行的多阶段民意调查的哪个阶段。我的计划是通过隐藏/显示各种 div 将其分为 6 个阶段,直到通过 Ajax 提交表单的最后一页。
回答by Leon
Not sure but I think, you can use
不确定,但我认为,你可以使用
$.getJSON('url', data, function(jsonData) {
// operate on return data (jsonData)
});
now you can access and operate on the PHP json data, if you're going to use it outside the getJson call you can assign it to a variable
现在您可以访问和操作 PHP json 数据,如果您打算在 getJson 调用之外使用它,您可以将它分配给一个变量
var neededData;
$.getJSON('url', data, function(jsonData) {
neededData = jsonData;
});
回答by ajtrichards
Try the jQuery documentation: http://api.jquery.com/jQuery.getJSON/
试试 jQuery 文档:http: //api.jquery.com/jQuery.getJSON/
This example should get you started:
这个例子应该让你开始:
$.getJSON('ajax/test.json', function(data) {
var items = [];
$.each(data, function(key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body');
});
This example is based on the JSON structure being;
此示例基于 JSON 结构;
{
"one": "Singular sensation",
"two": "Beady little eyes",
"three": "Little birds pitch by my doorstep"
}
回答by Rupesh Pawar
Do not use echo in PHP. It will print string not JSON.
不要在 PHP 中使用 echo。它将打印字符串而不是 JSON。
Use json_encodeto pass JSON to javascript.
使用json_encode将 JSON 传递给 javascript。
Use can use eachto get the values in JSON at javascript end.
使用可以使用each在 javascript 端获取 JSON 中的值。
Example
例子
http://www.darian-brown.com/pass-a-php-array-to-javascript-as-json-using-ajax-and-json_encode/
http://www.darian-brown.com/pass-a-php-array-to-javascript-as-json-using-ajax-and-json_encode/
回答by Claudio Bernasconi
If you are using JQuery there is a really simple solution to your approach as you can see here: http://api.jquery.com/jQuery.getJSON/.
如果您使用的是 JQuery,那么您的方法有一个非常简单的解决方案,如您所见:http: //api.jquery.com/jQuery.getJSON/。
Otherwise I just want you to explain that there is no way to access your JSON directly in JavaScript as you tried in your code above. The main point is, that JavaScript runs on your browser while your PHP script runs on your server. So there must definitely be a communication between them. So you have to request the data from the server over http I would suggest.
否则,我只是想让您解释一下,无法像您在上面的代码中尝试的那样直接在 JavaScript 中访问您的 JSON。重点是,JavaScript 在浏览器上运行,而 PHP 脚本在服务器上运行。所以他们之间肯定是有交流的。因此,我建议您必须通过 http 从服务器请求数据。
HTH
HTH