意外令牌:尝试解析JSON字符串时

编程入门 行业动态 更新时间:2024-10-27 21:19:23
本文介绍了意外令牌:尝试解析JSON字符串时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试解析此JSON字符串:

I am trying to parse this JSON string:

{ "RESULTS": [ { "name": "Thessaloniki GR", "type": "Sailing", "l": "/sailing-weather/beach:Porto%20Carras%20Marina 45904" }, { "name": "Thessaloniki, Greece", "type": "city", "c": "GR", "zmw": "00000.1.16622", "tz": "Europe/Athens", "tzs": "EET", "l": "/q/zmw:00000.1.16622" } ] }

从此处

这是我的摘录:

$(document).ready(function () { $("#w11").autocomplete({ source: function (a, b) { $.ajax({ url: "autocomplete.wunderground/aq", dataType: "jsonp", data: { format: "jsonp", query: a.term }, success: function (a) { for (i in data.RESULTS) { console.log(data.RESULTS); } } }) } }); });​

哪个在第一行是{ "RESULTS": [

如何解析JSON结果?

How can I parse the JSON Results?

推荐答案

您已经告诉jQuery期望 JSON-P ,而不是 JSON :

You've told jQuery to expect JSON-P, not JSON:

dataType: "jsonp"

...但是结果是JSON. JSON-P和JSON本质上是不同的.这是一个示例JSON响应:

...but the result is JSON. JSON-P and JSON are fundamentally different things. Here's an example JSON response:

{"foo": 42}

这是JSON-P响应的样子:

Here's what a JSON-P response might look like:

callback({"foo": 42});

callback({foo: 42});

如果autocomplete.wunderground/a与代码在其中运行的文档不在同一来源,则由于相同来源政策(除非相关服务器支持 CORS ,允许您发出请求的来源,并且用户使用的浏览器也支持CORS.我怀疑这是您尝试使用跨域工作的JSON-P的原因.事实是,服务器也必须支持JSON-P.尽管URL中的format=jsonp,服务器仍未使用JSON-P进行响应,而是使用JSON.

If autocomplete.wunderground/a is not on the same origin as the document your code is running in, you won't be able to retrieve JSON from it via ajax because of the Same Origin Policy (unless the server in question supports CORS, allows your origin for the request, and the browser the user has also supports CORS). Which I suspect is why you tried to use JSON-P, which works cross-origin. The thing is, though, that the server has to support JSON-P as well. Despite the format=jsonp in the URL, the server is not responding with JSON-P, but with JSON.

在评论中,您链接到他们的文档API ,表明它们 do 支持JSON-P,只是在URL中使用了非标准的参数名称(cb而不是更常见的callback)

In the comments, you linked to their docs for that API, which show that they do support JSON-P for it, just using a non-standard argument name in the URL (cb instead of the much more common callback).

这应该可以工作(我还解决了我在对该问题的评论中提到的代码问题):

So this should work (I've also fixed the issues with the code I mentioned in my comment on the question):

$.ajax({ url: "autocomplete.wunderground/aq", dataType: "jsonp", jsonp: "cb", // <================= New bit is here data: { format: "json", // <=== "json" not "jsonp" according to the docs, but I think the "cb" argument overrides it anyway query: a.term }, success: function (data) { // <=== `data`, not `a` var i; for (i in data.RESULTS) { console.log(data.RESULTS[i]); // <=== Use [i] here } } }); // <=== Semicolon was missing

实际上它确实有效:实时示例 | 来源

And in fact it does work: Live Example | Source

jsonp参数告诉jQuery使用哪个URL参数来定义JSON-P回调的名称.默认值为标准callback,但该API使用非标准参数.

The jsonp argument tells jQuery what URL parameter to use to define the name of the JSON-P callback. The default is the standard callback but that API uses a non-standard argument.

更多推荐

意外令牌:尝试解析JSON字符串时

本文发布于:2023-10-29 22:45:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1540985.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   字符串   意外   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!