0

I was looking at the autocomplete widget on www.healthgrades.com and inspected the network response.

I think the data is JSON, but it appears to have been run through some sort of encoding / filter and escaped, then returned in a jQuery tag (presumably a cache buster?).

A small bit of the data looks like what you see below.

jQuery17207977216457948089_1379039838014([{"ItemIds":null,"LinkUrl":"/hospital-directory/texas-tx-central/scott-and-white-memorial-hospital-hgst712bc8b6450054","Text":"Scott and White Memorial Hospital","PlainText":null,"Type":"tophospital","TrackingName":null,"StateAbbreviation":null,"Data":null},{"ItemIds":null,"LinkUrl":"/hospital-directory/texas-tx-austin/st-davids-medical-center-hgst613bc8b6450431","Text":"St. David\u0027s Medical Center","PlainText":null,"Type":"tophospital","TrackingName":null,"StateAbbreviation":null,"Data":null},{"ItemIds":null,"LinkUrl":"/hospital-directory/texas-tx-southern/st-davids-georgetown-hospital-hgst182bc8b6450191","Text":"St. David\u0027s Georgetown Hospital","PlainText":null,"Type":"tophospital","TrackingName":null,"StateAbbreviation":null,"Data":null},{"ItemIds":null,"LinkUrl":"/hospital-directory/texas-tx-austin/heart-hospital-of-austin-hgstbb7ecdaa450824","Text":"Heart Hospital of Austin","PlainText":null,"Type":"tophospital","TrackingName":null,"StateAbbreviation":null,"Data":null},{"ItemIds":null,"LinkUrl":"/hospital-directory/texas-tx-austin/st-davids-north-austin-medical-center-hgst234bc8b6450809","Text":"St. David\u0027s North Austin Medical Center","PlainText":null,"Type":"tophospital","TrackingName":null,"StateAbbreviation":null,"Data":null}]);

What are the benefits of doing this, and if my assumptions are wrong, what is going on here?

4

2 回答 2

2

它是jsonp请求的响应格式,它是一种用于克服 ajax 请求的同源 策略限制的技术,其中 ajax 请求到与页面加载位置不同的域被浏览器阻止。

在这种特殊情况下,它没有多大用处,因为页面和 ajax 资源都在同一个域中。

于 2013-09-13T02:53:58.110 回答
2

JSONP这是 jQuery支持的一个例子。

JSONP,如果你不熟悉的话,就是“带有 Padding 的 JSON ”。填充是对以 JSON 作为参数的全局函数的调用。

开头的名称jQuery17207977216457948089_1379039838014, 是 jQuery 在请求开始时生成的全局函数。并且,它将使用?查询字符串中的占位符为任何请求创建一个:

jsonp类型将查询字符串参数附加callback=?到 URL。服务器应在 JSON 数据前面加上回调名称,以形成有效的 JSONP 响应。我们可以指定一个参数名称,而不是callback使用 jsonp 选项$.ajax()

JSONP 的主要优点是它支持跨域请求。它通过创建一个<script>而不是一个XMLHttpRequest(类似于 using $.getScript())来做到这一点,因为它们不受SOP限制。但是,它们仅限于GET请求;所以这是一个权衡取舍。

而且,它是在引入CORS之前可用的跨域选项。

顺便说一句:JSONP 在技术上被 JSON 视为 JavaScript,利用了从 JavaScript 中获取的 JSON 语法。

于 2013-09-13T02:54:54.453 回答