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

时间:2011-03-29 23:02:28

标签: javascript ajax json jsonp

我已经实现了一个简单的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是这样的:    http://mywebserverdotcom/jsonp/data.php?resource=jsondata&callback=processJsonData

我看到以下形式的回复:

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

我的数据已经完成,一切都很好。

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

1)我添加了一个&lt; script&gt;我的HTML文件底部的元素,其中包含上面的URL

2)使用回调函数

实现了一个JS文件

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

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

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

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

任何帮助非常感谢!

由于

3 个答案:

答案 0 :(得分:2)

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

问题可能是服务器端代码中的某个地方。你可以发布你正在使用的PHP吗?

答案 1 :(得分:1)

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

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

在执行所有先前的脚本之前,不会执行脚本标记的JavaScript。当您的JSONP请求脚本执行时,它希望处理程序已经存在。但是如果在JSONP脚本之后才包含包含处理程序的脚本,那就太晚了。

答案 2 :(得分:0)

这是一个数据样本。我已经离开了前面的回调。这只是数据的一部分,所以结束]);不包括在内。这是公共数据。我知道它是有效的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"},