我有一個PHP的API,它返回一個JSON
數據,試圖接收JSON
從我的應用程序時,我得到了follwoing錯誤:問題與返回的JSON MIME類型
"Expected content type {(\n \"text/json\",\n \"application/json\",\n \"text/javascript\"\n)}, got text/html";
你可以回答說,PHP API ISN 「T返回有效JSON,好了,我檢查了JSON的皮棉,這是一個有效的JSON,另外,頭被設置爲JSON
header('Content-Type: text/json');
echo '{"Response":'.json_encode($arr).'}';
我還檢查MAMP mime.types
文件,並將其正確包括JSON MIME類型。
我唯一懷疑的是當我通過瀏覽器調用API時,我得到了預期的JSON,但是當頁面的源代碼被甩掉時,它在開始時顯示出2條空行。如下所示。
1-
2-
3- {"Response":[{"Product ID":"12037","Product Title":"RIng 14K Cloud 9","Product Description":"RIng 14K Cloud 9","Product Wholsale Price":"$255","Product Sale Price":"$460","Category Id":"55","Category Name":"Rings","Product Image":"http:\/\/192.163.1.87:8888\/xxxxxx\/wp-content\/uploads\/2012\/12\/150101.jpg","Product Sub Images":["http:\/\/192.163.1.87:8888\/xxxxx\/wp-content\/uploads\/2012\/12\/150101.jpg"],"SKU":"150101-50","Quantity":"1","Size":"50","Color":"Y","Manufacturer":"xxxxxx"},{"Product ID":"12039","Product Title":"Ring 14K Cloud 9","Product Description":"Ring 14K Cloud 9","Product Wholsale Price":"$255","Product Sale Price":"$460","Category Id":"55","Category Name":"Rings","Product Image":"http:\/\/192.163.1.87:8888\/xxxxx\/wp-content\/uploads\/2012\/12\/150101.jpg","Product Sub Images":["http:\/\/192.163.1.87:8888\/xxxxx\/wp-content\/uploads\/2012\/12\/150101.jpg"],"SKU":"150101-52","Quantity":"1","Size":"52","Color":"Y","Manufacturer":"xxxxxx"}]}
這是否與PHP代碼或MAMP服務器中的任何事情有關?提前Thanx。
你如何試圖消耗json數據,當你得到錯誤? – datasage 2013-03-27 22:17:03
將內容類型更改爲「application/json」 – Ibu 2013-03-27 22:18:20
json的正確標題是header('Content-type:application/json')'。試試看。 – PlantTheIdea 2013-03-27 22:18:23