当前位置: 代码迷 >> JavaScript >> JSONP是否对响应强加一些大小限制?
  详细解决方案

JSONP是否对响应强加一些大小限制?

热度:39   发布时间:2023-06-05 10:18:24.0

我已经实现了一个简单的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 . ');'; 
?>

当我手动输入url时,这非常有用。 如果我的URL是这样的:

我看到以下形式的响应:

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

我的数据很完整,一切看起来都很不错。

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

1)我在HTML文件的底部添加了<script>元素,并带有上面的URL

2)用回调函数实现一个JS文件

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

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

数据被切断,因此JSON格式混乱,并且没有close); 在函数调用的末尾,这会产生一个错误。 错误是:找不到processJsonData变量。

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

任何帮助,不胜感激!

谢谢

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

问题可能出在服务器端代码中。 您可以发布正在使用的PHP吗?

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

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

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

这是数据样本。 我留在前面的回调中。 这只是数据的一部分,所以结尾是]);。 不包括在内。 它是公共数据。 我知道它是有效的JSON,因为我使用的是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"},
  相关解决方案