javascript - 为什么这个网站返回包装在 jQuery 标签中的自动完成数据?

标签 javascript jquery ajax json unicode

我正在查看 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 of callback=? 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 than callback 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/

相关文章:

jquery - 将 jquery ajax 中的许多变量提交到 php 时遇到问题

javascript - Jquery/Ajax - 解析 JSON 响应

javascript - 无法通过自定义按钮刷新 Kendo Grid?

javascript - 在新选项卡中以可读格式显示 json

jquery - 显示 div 可以容纳什么

javascript - 如何用无限循环来显示这个函数?

jQuery Ajax 方法不返回 XHR 对象

javascript - 使用 jQuery(ajax、json)和 Spring MVC 发送多行表单数据

javascript - 使用 Jasmine 2.0 对 $scope.$evalAsync 函数进行单元测试

javascript - 使用 AJAX 实时搜索栏时出现“ERR_EMPTY_RESPONSE”