Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JSONP是否对响应施加了一些大小限制?_Javascript_Ajax_Json_Jsonp - Fatal编程技术网

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

Javascript JSONP是否对响应施加了一些大小限制?,javascript,ajax,json,jsonp,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

我实现了一个简单的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 . ');'; 
?>
我的数据是完整的,一切看起来都很好

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

1) 我用上面的URL在HTML文件的底部添加了一个元素

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

我犯了一个错误。我使用Web Inspector查看错误,它在回调上显示了一个错误,并且看起来回调在响应中大约有200个字符(我没有计算)被截断,因此现在响应是:

processJsonData([{"record_id":"317", ...
数据被切断,因此JSON格式混乱,无法关闭);在函数调用的末尾,这将创建一个错误。错误为:找不到processJsonData变量

所以。。。或者我只是做错了,或者通过script元素使用JSONP回调时允许的响应大小有一些大小限制,或者其他一些我没有想到的事情

非常感谢任何帮助


谢谢

不,关于使用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"},

确保在包含回调函数的脚本之后包含JSONP响应脚本。错误消息似乎表明脚本标记出现了问题。脚本标记的顺序应如下所示:

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


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

我包含了,整个PHP都在上面。@Elisabeth-酷,你能同时发布
jsonFile
的内容吗?这有点像是在暗中拍摄,但我想知道它是否有一个换行符或其他字符,它正在破坏JavaScript解析器。啊。看起来都是正确转义的,但可能缺少反斜杠…好的,在Web检查器中它被截断的位置可能只是一个任意点,调试器不会显示长度超过X的字符串。事实证明,数据中有各种类型的\n。这是数据的一个小样本,您可以看到\n。不知道是不是这样?只要没有文字换行符,数据中有
\n
就可以了。给出了错误消息,似乎您只是指定了错误的回调函数?在原始帖子中的示例中,您的回调函数名为
processJsonData()
,但它试图调用名为
callback()
的回调函数的PHP输出。谢谢您,原来我在错误的位置定义了回调函数。(名字的变化是我只是在试验,看看我是否能弄明白)。非常感谢你的帮助!!!!如果您对JSON不确定,可以运行它。它将捕获几乎所有格式错误的JSON。好吧,伙计,我感觉我有一个d'oh时刻。我对脚本元素进行了正确的排序,但我将回调函数嵌套在window.onload中,完全忘记了它不仅在onload完成时定义,而且作为onload函数范围之外的回调函数不可见。非常感谢。
<script type="text/javascript" src="myscript.js" />
<script type="text/javascript" src="jsonprequest.php?callback=processJsonData&arg=1" />