【问题标题】:Does JSONP impose some size limit on the response?JSONP 是否对响应施加了一些大小限制?
【发布时间】:2011-07-25 16:42:12
【问题描述】:

我已经实现了一个简单的 PHP 脚本来为一组我希望通过跨域请求访问的 JSON 文件提供 JSONP 支持。

这里是:

<?php
$jsonFile = $_GET['resource'] . ".json";
$fh = fopen($jsonFile, 'r');
$jsonData = fread($fh, filesize($jsonFile));
fclose($fh);

$jsonData = trim($jsonData);
header("Content-type: application/json");  
echo $_GET['callback'] . '(' . $jsonData . ');'; 
?>

当我手动输入网址时,这非常有用。 如果我的网址类似于: http://mywebserverdotcom/jsonp/data.php?resource=jsondata&callback=processJsonData

我看到以下形式的响应:

processJsonData([{"record_id":"317", ...}]);

我的数据是完整的,一切看起来都很好。

但是,当我在我的 HTML/JS 中使用以下方法尝试此操作时:

1) 我在 HTML 文件的底部添加了一个

2) 实现了一个带有回调函数的JS文件

我得到一个错误。我使用Web Inspector查看错误,它在回调中显示错误,并且看起来回调在响应中大约200个字符左右(我没有计算)字符被切断,所以响应是现在:

processJsonData([{"record_id":"317", ...

数据被截断,所以JSON格式乱了,没有关闭);在函数调用结束时,这会产生错误。错误是:找不到 processJsonData 变量。

所以...要么我只是做错了,要么通过脚本元素使用 JSONP 回调允许的响应大小有一些大小限制,或者我没有想到的其他东西....

非常感谢任何帮助!

谢谢

【问题讨论】:

  • 如果您不确定您的 JSON,您可以通过 jsonlint.com 运行它。它会捕获几乎所有格式错误的 JSON。

标签: javascript ajax json jsonp


【解决方案1】:

确保您的 JSONP 响应脚本包含在 包含回调函数的脚本之后。错误消息似乎表明您的脚本标签乱序。您的脚本标签应按如下顺序排列:

<script type="text/javascript" src="myscript.js" />
<script type="text/javascript" src="jsonprequest.php?callback=processJsonData&arg=1" />

脚本标签的 JavaScript 直到所有之前的脚本都执行完毕后才会执行。当您的 JSONP 请求脚本执行时,它期望处理程序已经存在。但是如果在 JSONP 脚本之后才包含包含处理程序的脚本,那就太迟了。

【讨论】:

  • 好吧,男孩,我觉得我有一个很好的时刻。我的脚本元素排序正确,但我将回调函数嵌套在 window.onload 中,完全忘记了它不仅会在 onload 完成时定义,而且在 onload 函数范围之外作为回调函数不可见。谢谢!
【解决方案2】:

以下是数据样本。我已经在前面的回调中离开了。这只是数据的一部分,所以结尾 ]);不包括在内。是公开数据。我知道它是有效的 JSON,因为我使用 exact 相同的文件,使用 Ajax 而不是 JSONP 从我的本地计算机加载它,它工作正常。只有当从远程服务器通过此 JSONP/PHP 脚本访问时,它才会因错误而失败。确切的错误信息是:

ReferenceError: Can't find variable: callback

错误的位置是 data.php:1,这是我的远程 PHP 脚本。

callback([{"record_id":"317","artist":"Vern Luce","title":"Untitled","date":"1983","medium":"Painted steel","discipline":"sculpture","dimensions":"a: 93 \" x 40 \" x 64 \", b: 76.5 \" x 31 \" x 29 \", c: 48.5 \" x 85 \" x 20 \"","funding_source":"CETA","location":"MacLeay Park","street":"NW 29th Ave and Upshur St","city":"Portland","state":"OR","zipcode":"","lat":"45.535999799999999","lng":"-122.7110045","description":"Three geometric abstract steel sculptures are placed in a raised landscaped area in and located directly south of the Thurman Street Bridge.  In siting the work, the artist wanted the sculptures to respond both to the surrounding greenspace (thus, the bright red color) and to the broad horizontal expanse of the Thurman Street bridge (thus, the vertical nature of the sculptures).  At the time the pieces were installed, Vern Luce lived near Lower MacLeay Park and selected the site both for its visual beauty and its proximity to his home.\n\nProject History\nThe Comprehensive Education Training Act of the early 70's provided grants to a number of Portland artists that enabled them to create artwork.  As a result, over 500 works by 52 artists became part of the City of Portland's collection, providing a rich and diverse history of art in Portland.  Aside from Lower MacLeay Park, two other Portland parks feature permanent sculptures acquired through this program: a sculpture by Bruce West in Lair Hill Park and a piece by Jerry Allen in Peninsula Park.","image_url":"http:\/\/data.racc.org\/pa_inventory\/0240\/0240thumb.jpg","detail_url":"http:\/\/racc.org\/public-art\/search\/?recid=317.192","date_modified":"2010-07-19 00:00:00"},{"record_id":"359","artist":"Bruce West","title":"BW1","date":"1978","medium":"Cor-ten steel","discipline":"sculpture","dimensions":"6' x 30' x 20'","funding_source":"CETA 1976-77","location":"Lair Hill Park","street":"3000 SW Barbur Blvd","city":"Portland","state":"OR","zipcode":"97201","lat":"45.501570100000002","lng":"-122.68130650000001","description":"","image_url":"http:\/\/data.racc.org\/pa_inventory\/0098\/0098thumb.jpg","detail_url":"http:\/\/racc.org\/public-art\/search\/?recid=359.185","date_modified":"2010-12-29 00:00:00"},{"record_id":"362","artist":"Jerry  Allen","title":"Disc #4","date":"1979","medium":"Cast silicon bronze","discipline":"sculpture","dimensions":"diameter: 4 1\/2'","funding_source":"CETA 1977-78","location":"Peninsula Park","street":"6222 N. Albina  Avenue","city":"Portland","state":"OR","zipcode":"97217","lat":"45.568221899999998","lng":"-122.6748716","description":"","image_url":"http:\/\/data.racc.org\/pa_inventory\/0102\/0102thumb.jpg","detail_url":"http:\/\/racc.org\/public-art\/search\/?recid=360.55","date_modified":"2010-03-12 00:00:00"},

【讨论】:

  • 在数据中有\n 应该没问题,只要没有文字换行符。鉴于您的错误消息,您似乎只是指定了错误的回调函数?在您原始帖子中的示例中,您的回调称为processJsonData(),但您的PHP 输出试图调用名为callback() 的回调。
  • 谢谢aroth,原来我在错误的地方定义了我的回调函数。 (改名是我只是在试验,看看我能不能弄明白)。非常感谢您的帮助!!!!
【解决方案3】:

不,使用 JSONP 不会限制响应的大小。就 HTTP 传输层而言,您只是将一些文本数据从服务器发送到客户端;它并不关心文本数据是什么或内部结构如何。

问题可能出在服务器端代码的某个地方。你能把你正在使用的PHP贴出来吗?

【讨论】:

  • 我做了,整个PHP都在上面了。
  • @Elisabeth - 很酷,你能把jsonFile 的内容也发一下吗?这有点像在黑暗中拍摄,但我想知道它是否有一个换行符或其他字符正在搞砸 JavaScript 解析器。
  • 啊。好吧,看起来一切都已正确转义,但也许缺少反斜杠...
  • 好的,它在 Web Inspector 中被截断的地方可能只是调试器不会显示长度超过 X 的字符串的任意点。
  • 数据里有各种\n,原来如此。这是数据的一个小样本,你可以看到\n。不知道是不是这样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
  • 2011-11-18
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多