2011-03-29 112 views
1

我已經實現了一個簡單的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回調通過腳本元素允許的響應大小上存在一定的大小限制,或者其他我沒有想到的... 。

任何幫助非常感謝!

謝謝

+0

如果您不確定自己的JSON,可以通過http://www.jsonlint.com/運行。它會捕獲幾乎任何格式不正確的JSON。 – Thanatos 2011-03-30 00:28:37

回答

1

確保您的JSONP響應腳本包括含有回調函數腳本。錯誤消息似乎表明您的腳本標籤出現故障。腳本標記應當責令這樣的:直到所有先前的腳本已經執行不執行

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

腳本標籤的JavaScript。當您的JSONP請求腳本執行時,它預期處理程序已經存在。但是,如果包含處理程序的腳本在JSONP腳本之後才被包含,那就太遲了。

+0

好吧,男孩我覺得我有一個美好的時刻。我有正確的腳本元素排序,但我將回調函數嵌套在window.onload中,並且完全忘記了它不僅在onload完成時定義,而且作爲onload函數範圍之外的回調函數不可見。謝謝! – Elisabeth 2011-03-30 02:07:02

2

不,沒有關於使用JSONP應該限制您的響應的大小。就HTTP傳輸層而言,你只是將一些文本數據從服務器發送到客戶端;它並不關心那些文本數據是什麼或者它是如何在內部構建的。

可能問題出在服務器端代碼的某個地方。你能發佈你正在使用的PHP嗎?

+0

我做過了,整個PHP都在上面。 – Elisabeth 2011-03-29 23:19:46

+0

@伊麗莎白 - 酷,你可以發佈'jsonFile'的內容嗎?這在黑暗中是一種鏡頭,但是我想知道是否它有一個換行符或其他字符,它是搞砸了JavaScript解析器。 – aroth 2011-03-29 23:30:51

+0

啊。以及它看起來好像全部正確地逃脫了,但也許有一個反斜槓缺失...... – Elisabeth 2011-03-29 23:47:12

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

只要沒有文字換行符,數據中的\ n應該沒問題。鑑於你的錯誤消息,它似乎也許你只是指定錯誤的回調函數?在原始文章中的示例中,您的回調稱爲「processJsonData()」,但您的PHP的輸出嘗試調用名爲callback()的回調。 – aroth 2011-03-30 00:05:49

+0

謝謝你,事實證明我已經在錯誤的地方定義了我的回調函數。 (名稱變化是我只是試驗,看看我能否弄清楚)。非常感謝你的幫助! – Elisabeth 2011-03-30 02:08:33

相關問題