我正在查看 www.healthgrades.com 上的自动完成小部件并检查网络响应。
我认为数据是 JSON,但它似乎已经通过某种编码/过滤器运行并转义,然后在 jQuery 标记中返回(大概是缓存破坏器?)。
一小部分数据如下所示。
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}]);
这样做有什么好处,如果我的假设是错误的,那么这里发生了什么?
最佳答案
这是 jQuery JSONP
的一个示例支持。
如果您不熟悉,JSONP 是“JSON with Padding ”。 padding 是对以 JSON 作为参数的全局函数的调用。
开头的名称,jQuery17207977216457948089_1379039838014
,是 jQuery 在请求开始时生成的全局函数。并且,它会使用 ?
placeholder in the query-string 为任何请求创建一个。 :
The
jsonp
type appends a query string parameter ofcallback=?
to the URL. The server should prepend the JSON data with the callback name to form a valid JSONP response. We can specify a parameter name other thancallback
with the jsonp option to$.ajax()
.
JSONP的主要优点是支持跨域请求。它通过创建 <script>
来实现这一点。而不是XMLHttpRequest
(类似于使用 $.getScript()
)因为它们不受 SOP 的限制。然而,它们仅限于 GET
要求;所以这是一个权衡。
并且,它是在引入 CORS 之前可用的跨域选项。 .
顺便说一句:从技术上讲,JSONP 是将 JSON 视为 JavaScript,利用了取自 JavaScript 的 JSON 语法。
关于javascript - 为什么这个网站返回包装在 jQuery 标签中的自动完成数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18777469/