简体   繁体   English

JSON parseError-以字符串形式获取json

[英]JSON parseError - get json as string

I'm trying to get json data from a service, but getting parseError when I use $.ajax with datatype 'JSONP': 我正在尝试从服务获取json数据,但是当我将$ .ajax与数据类型'JSONP'一起使用时,会遇到parseError:

$.ajax({
    url: url,
    dataType: 'JSONP'
})
.error(function(XMLHttpRequest, textStatus, errorThrown) { ... })
.done(function(data) { ... });

If i try it with other datatype than 'JSONP' it returns 404 error. 如果我尝试使用“ JSONP”以外的其他数据类型返回404错误。

How can i get just a string instead of parsing json, i believe there is some linebreaks in json that cause parse errors. 我怎么能只得到一个字符串而不是解析json,我相信json中有一些换行符会导致解析错误。

Here is the fiddle http://jsfiddle.net/FSEZQ/3/ 这是小提琴http://jsfiddle.net/FSEZQ/3/

That's JSON, not JSONP. 那是JSON,而不是JSONP。

For example, this is JSON: 例如,这是JSON:

{"key": "value"}

This is JSONP: 这是JSONP:

callback({"key": "value"})

If the service doesn't provide JSONP, the browser prevents you from getting it (same origin security restrictions). 如果该服务不提供JSONP,浏览器将阻止您获取它(相同的来源安全限制)。

The way people get around same origin restrictions consist of some server utilization. 人们绕过相同原点限制的方式包括一些服务器利用率。 You can either right code that does this in PHP, or use a service such as AnyOrigin . 您可以对在PHP中执行此操作的代码进行修改,也可以使用诸如AnyOrigin之类的服务。

Here's an AnyOrigin example. 这是一个AnyOrigin示例。

$.getJSON('http://anyorigin.com/get?url=metservice.com/publicData/tides2MonthAuckland&callback=?', function (data) {
    $('#result1').html(JSON.stringify(data.contents));
}).fail(function (XMLHttpRequest, textStatus, errorThrown) {
    $("#result2").html(textStatus);
});

... and an accompanying fiddle . ...和伴随的小提琴

Here's an example that shows how this data can be used. 这是显示如何使用此数据的示例

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

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