开发者

Does JSONP impose some size limit on the response?

I have implemented a simple PHP script to provide JSONP support for a set of JSON files I want to be accessible via cross-domain requests.

Here it is:

<?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 . ');'; 
?>

This works great when I type the url in manually. If my URL is something like: http://mywebserverdotcom/jsonp/data.php?resource=jsondata&callback=processJsonData

I see the response in the form of:

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

and my data is complete and everything looks good.

However, when I try this using the following method in my HTML/JS:

1) I added a <script> element at the bottom of my HTML file with the URL above

2) Implemented a JS file with the callback function

I get an error. I used Web Inspector to see the error, and it shows an error on the callback, and it looks li开发者_开发知识库ke the callback is cut off at about 200 characters or so (I didn't count) characters into the response, so the response is now:

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

The data is cut off, so the JSON format is messed up and there is no closing ); at the end of the function call, which creates an error. The error is: processJsonData variable not found.

So... Either I'm just doing this all wrong, OR there is some size limit in the response size allowed using a JSONP callback via the script element, OR something else I'm not thinking of....

Any help much appreciated!

Thanks


No, nothing about using JSONP should be limiting the size of your response. As far as the HTTP transport layer is concerned you are just sending some text data from the server to the client; it doesn't really care what that text data is or how it is structured internally.

Probably the issue is somewhere in the server-side code. Can you post the PHP you are using?


Make sure your JSONP response script is included after your script containing the callback function. The error message seems to indicate you had the script tags out of order. Your script tags should be ordered like this:

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

A script tag's JavaScript is not executed until all previous scripts have executed. When your JSONP request script executes, it expects the handler to already exist. But if your script containing the handler isn't included until after the JSONP script, that is too late.


Here's a sampling of the data. I've left in the callback at the front. This is just part of the data, so the ending ]); are not included. It's public data. I know it's valid JSON because I'm using the exact same file using Ajax instead of JSONP to load it from my local machine and it works fine. It's only when accessing via this JSONP/PHP script from a remote server that it fails with the error. The exact error message is:

ReferenceError: Can't find variable: callback

and the location of the error is data.php:1 which is my remote PHP script.

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"},
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜