5

我正在使用 jQuery 的 ajax 函数来访问跨域 url。该网址将返回 json。我正处于发现阶段,但我认为,因为 json 值包含几个 '"' 字符串,所以 json eval 会引发错误并停止执行我的客户端脚本。我得到的错误是“未终止的字符串文字”。我知道,如果我通过将返回的 json 放在本地 div 中并删除“”来对返回的 json 进行硬编码并删除 字符串(和几个隐藏的特殊字符),我可以让 json 成功评估。无论是通过解决方法,还是通过更正我的代码,我能做些什么来从 url 获取 json 并将其作为有效的 json 对象存储在我的客户端 javascript 中?

我当前的代码。没有任何定义的函数(成功、完成、错误、数据过滤器)执行:

$(function() {
  $.ajax({
  url: "http://www.codemash.org/rest/sessions.json?format=jsonp&callback=?", 
  dataType: "jsonp",
  success: successFunc,
  complete: completeFunc,
  error: errorFunc,
  dataFilter: dataFilterFunc
});

});

function successFunc() { console.log('successFunc(). enter.'); }
function completeFunc() { console.log('complete(). enter.'); }
function errorFunc() { console.log('errorFunc(). enter.'); }
function dataFilterFunc(data, type) { 
  data.replace(/\W/g, ' ');
  return data; 
}
4

5 回答 5

3

问题是 JSONP 服务,它完全取消了callback参数。

JSONP 响应只是一个使用 get 参数作为函数名的函数调用callback

如果你查看一些 JSONP 服务的响应,它应该是这样的:

http://somesite.com/jsonp?callback=myCallback _

将返回:

myCallback({/*json*/});

虽然您发布的返回的是普通 有效的JSON,但它不能作为JSONP响应处理。

于 2009-11-28T05:27:11.883 回答
1

Hmm. I don't know jQuery, but I know JSON. (I wrote a C++ based parser for it.) I went to the url you show: http://www.codemash.org/rest/sessions.json?format=jsonp&callback=?. Python's JSON parser accepts that, and my experience is that Python's JSON parser is correct. Note that this:

{ "blah": "string " More more more" }

...is valid JSON. The " part gets no special interpretation in JSON - they are just characters in the string.


Edit: Looking harder at this, I find it very interesting that whatever is generating this is escaping some forward slashes, but not all. The "Start" items are "/Date(-XXXXX)/", which is the text /Date(-XXXXX)/ - / in JSON can optionally be escaped. However, in items with keys "SpeakerURI" and "URI", they are not escaped.


Edit: Ran this through my C++ based parser. Initially it did not take it, but this JSON actually exposed a bug in my parser.

于 2009-11-28T04:25:38.507 回答
0

我似乎没有更接近客户端解决方案。我将代码更改为使用 jQuery-jsonp。现在看起来像这样:

$(function() {
  $.jsonp({
  url: "http://www.codemash.org/rest/sessions.json?format=jsonp&callback=?", 
  async: false,
  dataType: "jsonp",
  success: successFunc,
  complete: completeFunc,
  error: errorFunc,
  dataFilter: dataFilterFunc
});

});

function successFunc(json, textStatus) { 
  console.log('successFunc(). enter.'); 
}
function completeFunc(xOptions, textStatus) { 
  console.log('complete(). textStatus: ['+textStatus + ']'); 
}
function errorFunc(xOptions, textStatus) { 
  console.log('errorFunc(). textStatus: [' + textStatus + ']'); 
}
function dataFilterFunc(j) { 
  console.log('dataFilterFunc(). enter.');
  return j; 
}

Firebug 的控制台日志显示:

X unterminated string literal
[{"URI"..... ( the json returned from the server
errorFunc(). textStatus: [error]
complete(). textStatus: [error]

无论出于何种原因,我的 Firefox 浏览器(FF v3.0.15,Linux Ubuntu 9.0.4)中的 json 解析都会引发“未终止的字符串文字”异常。我知道如果我删除“可维护的 ASP.NET MVC”块中的“& quo t”字符串和两个字符,那么 json 将成功解析。由于我无法控制服务器端,我可以用客户端 javascript 做些什么,最终将服务器的 json 字符串按原样获取到客户端 json 对象中。我认为 jsonp 的 dataFilter 属性正是为此目的而设置的,以拦截 json 并“清理”它。但是,我的代码中的那个函数没有被调用,很可能是因为传递给 dataFilterFunc 函数的 json 不会成功解析。

于 2009-11-28T19:12:05.153 回答
0

You might consider trying the jquery-jsponp plugin, which fixes some problems with jQuery's jsonp handling.

http://code.google.com/p/jquery-jsonp/

I use it and there are a lot of great examples.

You really need to get your callbacks working so you can see what's going on.


Update: I took a stab with jsonp and I'm getting unexpected token errors. Not sure why--I can grab the data manually from the URL, assign it to a JavaScript variable in the console, and it's just fine.

于 2009-11-28T04:34:35.570 回答
0

在分析了一个小时的数据后,

我发现以下

{
   "URI":"/rest/sessions/Maintainable-ASPNET-MVC",
   "Title":"Maintainable ASP.NET MVC",
   "Abstract":".....\u003e\u2028Duration: 60-90 minutes\u003cbr\u003e\u2028Subject Area: development....."
}

在 Duration 和 Subject Area 之前有 char \u2028,

\u2028 是行分隔符,所以它的意思是,有点 \n,所以你在引号“”之间有换行符,这可能是根案例。

如果您删除它,您将不会在萤火虫中看到未终止的字符串文字。

为了测试这一点,在地址栏中粘贴以下内容,Firefox 和 Opera 失败,而 IE8 和 Chrome 正在运行

javascript:alert(eval('[{"Abstract":"\u003e
    Duration: 60-90 minutes\u003cbr\u003e
    Subject Area: development."}]'))

有人已经在博客上写过

http://bulknews.typepad.com/blog/2009/02/json-jquery-and-some-unicode-characters-u2028.html

于 2009-11-28T06:03:51.630 回答