JSONP是否对响应施加了一定的大小限制?
我已经实现了一个简单的PHP脚本来为我想通过跨域请求访问的一组JSON文件提供JSONP支持。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的伟大工程。 如果我的网址是一样的东西: http://mywebserverdotcom/jsonp/data.php?resource=jsondata&callback=processJsonData
我看到的形式响应:
processJsonData([{"record_id":"317", ...}]);
,我的数据是完整的,一切看起来不错。
然而,当我尝试此使用下面的方法在我的HTML/JS:
1)I加入<脚本>元件在与所述URL我的HTML文件的底部上方
2)用回调函数实现了一个JS文件
我得到一个错误。我使用Web Inspector来查看错误,并在回调中显示错误,并且看起来回调被切断了大约200个字符(我没有统计)到响应中,所以现在的响应是:
processJsonData([{"record_id":"317", ...
数据被截断,所以JSON格式乱了,没有关闭);在函数调用结束时,会产生错误。错误是:找不到processJsonData变量。
因此......要么我只是做这一切都是错误的,或者在使用JSONP回调通过脚本元素允许的响应大小上存在一定的大小限制,或者其他我没有想到的... 。
任何帮助非常感谢!
谢谢
确保您的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函数范围之外的回调函数不可见。谢谢! – Elisabeth 2011-03-30 02:07:02
不,没有关于使用JSONP应该限制您的响应的大小。就HTTP传输层而言,你只是将一些文本数据从服务器发送到客户端;它并不关心那些文本数据是什么或者它是如何在内部构建的。
可能问题出在服务器端代码的某个地方。你能发布你正在使用的PHP吗?
这是数据的一个样本。我留在前面的回调。这只是数据的一部分,所以结尾]);不包括在内。这是公共数据。我知道这是有效的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"},
如果您不确定自己的JSON,可以通过http://www.jsonlint.com/运行。它会捕获几乎任何格式不正确的JSON。 – Thanatos 2011-03-30 00:28:37