[英]getJSON not returning anything
The JSON function below is not returning anything... 下面的JSON函数不返回任何内容...
$.getJSON(formUrl, function(data) {
alert(data)
$('.response').html('<p>' + data.title + '</p>'
+ '<p>' + data.description + '</p>');
});
the formUrl is correct and returning data in the form of formUrl是正确的,并且以以下形式返回数据
{"title":"Smashing Magazine","description":"Smashing Magazine is focused on design and web-development. We deliver useful information, latest trends and techniques, useful ideas, innovative approaches and tools."}
Can someone point out what I'm doing wrong? 有人可以指出我做错了吗? I'm alerting the data and its empty as well...
我正在提醒数据及其空白...
Thanks! 谢谢!
Are you calling $.getJSON
with a cross-domain URL (ie. from localhost to somewhere else)? 您是否要使用跨域URL调用
$.getJSON
(即从本地主机到其他地方)? That won't work unless you use the JSONP dataType. 除非您使用JSONP数据类型,否则这将无法工作。
Since it's not of the same origin , you need to add the Access-Control
headers to the JSON response. 由于它的来源不同 ,因此您需要将
Access-Control
标头添加到JSON响应中。
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
Do not break the string in two lines, it will result in error and may be the reason of Javascript not running. 不要将字符串分成两行,这将导致错误,并且可能是Javascript无法运行的原因。
Try something like this: (Notice how I did not break the content inside html() in two lines) 尝试这样的事情:(注意我没有在两行中中断html()内的内容)
$.getJSON(formUrl, function(data) {
alert(data);
$('.response').html('<p>' + data.title + '</p>' + '<p>' + data.description + '</p>');
});
Try to configure your server to deliver the page fetch.php
with a contentType application/json
尝试配置您的服务器以提供带有contentType
application/json
的页面fetch.php
Also just for a test, you can try to put your JSON into a .../fetch.js
file and call it with $.getJSON
. 同样为了进行测试,您可以尝试将JSON放入
.../fetch.js
文件中,并使用$.getJSON
调用。 It should work, as .js files are application/javascript
by default on most servers. 它应该可以正常工作,因为大多数服务器默认情况下.js文件是
application/javascript
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.