JSON parseError-以字符串形式获取json [英] JSON parseError - get json as string

查看:841
本文介绍了JSON parseError-以字符串形式获取json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从服务获取json数据,但是当我将$ .ajax与数据类型'JSONP'一起使用时会得到parseError:

I'm trying to get json data from a service, but getting parseError when I use $.ajax with datatype 'JSONP':

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

如果我尝试使用"JSONP"以外的其他数据类型返回404错误.

If i try it with other datatype than 'JSONP' it returns 404 error.

我怎么能只得到一个字符串而不是解析json,我相信json中有一些换行符会导致解析错误.

How can i get just a string instead of parsing json, i believe there is some linebreaks in json that cause parse errors.

这是小提琴 http://jsfiddle.net/FSEZQ/3/

推荐答案

这是JSON,而不是JSONP.

That's JSON, not JSONP.

例如,这是JSON:

{"key": "value"}

这是JSONP:

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

如果该服务不提供JSONP,浏览器将阻止您获取它(相同的源安全限制).

If the service doesn't provide JSONP, the browser prevents you from getting it (same origin security restrictions).

人们绕过相同原籍限制的方式包括一些服务器利用率.您可以对在PHP中执行此操作的代码进行修改,也可以使用诸如AnyOrigin之类的服务.

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 .

这是一个AnyOrigin示例.

Here's an AnyOrigin example.

$.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);
});

...和伴随小提琴.

下面是一个示例,该示例显示了如何使用此数据.

Here's an example that shows how this data can be used.

这篇关于JSON parseError-以字符串形式获取json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆