简体   繁体   English

从网站获取JSON并导入Javascript

[英]Getting JSON from website and into Javascript

I am trying to get some JSON data from the Guardian api, like from http://content.guardianapis.com/search?q=Tim%20Farron%20mp&page-size=3&format=json this example - by using a php program i have written. 我正在尝试从Guardian api获取一些JSON数据,例如从http://content.guardianapis.com/search?q=Tim%20Farron%20mp&page-size=3&format=json此示例-通过使用我拥有的php程序书面。 When run on its own from the data my javascript sends it the data ispresented normally as seen above, however when the javascript runs it is not getting anything back from the php - the code gets stuck. 从数据上单独运行时,我的JavaScript会正常发送数据,如上所示,但是,在运行javascript时,它并没有从php返回任何内容-代码被卡住了。

PHP (working) code: PHP(有效)代码:

<?php
//gets url from passed over value
$url = urldecode($_GET['url']);
//gets  contents of url
$results = file_get_contents($url);
header('content-type: text/json');
echo $results;

This is the part of my javascript which is not working - no response is returned as it gets stuck on the first line. 这是我的javascript不能正常工作的部分-由于卡在第一行,因此不会返回任何响应。

$.getJSON('getSTORY.php?url='+ url2, function(response) {
 console.log(response);
 });

In the console this is an error message which is returned ( i think!) 在控制台中,这是一条返回的错误消息(我认为!)

XHR finished loading: "http://yrs2013.bugs3.com/mpapp/getSTORY.php?url=http%3A%2F%2Fcontent.guardianapis.com%2Fsearch%3Fq%3DTim%20Farron%20mp%26page-size%3D3%26format%3Djson". jquery.js:6
//^above was the link using the php. when pressed this shows the required JSON code
send jquery.js:6
x.extend.ajax jquery.js:6
x.(anonymous function) jquery.js:6
x.extend.getJSON jquery.js:6
(anonymous function) script.js:57
c jquery.js:4
p.fireWith jquery.js:4
k jquery.js:6
r

I would like to know why the program is getting stuck and how to solve it, and also how to pass the variable back from php into javascript! 我想知道为什么程序被卡住,如何解决,以及如何将变量从php传递回javascript! I am very new to all this so thanks in advance! 我对此很陌生,所以在此先感谢! :) :)

Your code should work just fine but use this as reference: 您的代码应该可以正常工作,但可以将其用作参考:

$(function() { $('#test').bind('click', function(ev) { var url2="http://content.guardianapis.com/search?q=Tim%20Farron%20mp&page-size=3&format=json"; $.getJSON('test.php?url='+ url2, function(response) { console.log(response); $("#results").text(""+JSON.stringify(response)); if(response.response.status == "ok"){ $("#results").text(""); $.each(response.response.results, function(index, element) { $("#results").append($('<div>', { text: "Section: " + element.sectionName })); $("#results").append($('<div>', { text: "Title: " + element.webTitle })); $("#results").append($('<hr>')); }); $("#results").show(); } }); }); }); <form> <input type="button" name="test" id="test" value="test"> </form> <div id="results"> </div>

So your problem most likely is that your json response is invalid due to an output of $url which is not present in the above code. 因此,您的问题很可能是由于$url的输出导致您的json响应无效,上述代码中没有此输出。 So please for the future: Don't remove too much code for posting it in the question, also if it's just code for debugging purpose. 因此,请以后使用:不要在问题中删除过多的代码来发布它,也不要仅将其用于调试目的。

Remove every output before and after echo $results; 删除echo $results;之前和之后的所有输出echo $results;

What you should change too, is the content-type of json which is application/json : 您还应该更改的是json的内容类型,即application/json

header('content-type: application/json');

Then for checking if the response can be handled by the JSON parser you can use jsonlint which validates the json. 然后,为了检查响应是否可以由JSON解析器处理,您可以使用jsonlint来验证json。

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

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