简体   繁体   English

验证JSON时出现getJSON解析错误

[英]getJSON parse error on validating JSON

I am trying to use some JSON with JS that is output by a PHP script I control. 我试图使用一些JSON与JS,由我控制的PHP脚本输出。 The output validates fine at http://jsonlint.com/ but when I try to use it with $.getJSON it returns a parse error: 输出在http://jsonlint.com/上验证正常,但是当我尝试将它与$ .getJSON一起使用时,它会返回一个解析错误:

This is the non-descriptive error: Uncaught SyntaxError: Unexpected token : 这是非描述性错误: Uncaught SyntaxError: Unexpected token :

This is the source of the JSON: http://novaramedia.com/api/txt/ 这是JSON的来源: http//novaramedia.com/api/txt/

And this is my jQuery: 这是我的jQuery:

$.getJSON('http://novaramedia.com/api/txt/?callback=?', function(data) {

    console.log(data);

}).error(function(jqXHR, textStatus, errorThrown) {

    console.log(textStatus);
    console.log(errorThrown);

});

The errorThrown variable logs as: Error {} errorThrown变量记录为: Error {}

The JSON is output in a very normal basic way, with a PHP array input into json_encode. JSON以非常正常的基本方式输出,PHP数组输入到json_encode。 Here are the output lines of the script: 以下是脚本的输出行:

header("Content-type: application/json");
die(json_encode($output));

The PHP script is almost exactly the same as one used for another JSON endpoint [ http://novaramedia.com/api/fm/ ] which getJSON doesnt have problems with. PHP脚本几乎与用于另一个JSON端点[ http://novaramedia.com/api/fm/ ]的脚本完全相同,getJSON没有问题。 The only real difference is the value of 'content' which is HTML straight from a wordpress post content. 唯一真正的区别是“内容”的价值,它直接来自wordpress帖子内容。

Very frustrating problem so any help much appreciated :} 非常令人沮丧的问题所以任何帮助非常赞赏:}

For JSONP you need to have a callback function prepended. 对于JSONP,您需要预先设置回调函数。

Example: 例:

jQuery({...})

The endpoint such as http://novaramedia.com/api/txt/?callback=jQuery should return a response between jQuery( ... json output ... ) . 诸如http://novaramedia.com/api/txt/?callback=jQuery的端点应该在jQuery( ... json output ... )之间返回响应。

To expand: 扩张:

You could use header('Access-Control-Allow-Origin: \\'http://remote-site.com\\''); 您可以使用header('Access-Control-Allow-Origin: \\'http://remote-site.com\\''); and then access your data freely from another domain. 然后从另一个域自由访问您的数据。

The advantage would be that other people wouldn't be able to use jsonp on it, but maybe that was your intention in the first place. 优点是其他人不能使用jsonp,但也许这是你的意图。

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

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