我知道同一主題是由幾個人問的,但我無法從這些問題中找到我的問題的答案。 我有下面的代碼,SyntaxError:JSON.parse:空結果的意外字符
$.post("show_search_results.php", {location_name: ""+location_name+"", key: ""+key+"", category_id: ""+category_id+"", page_number: ""+page_number+""}, function(data){
if(data.length >0){
var dataArray = JSON.parse(data);
var result_count=(dataArray.root.data.partners).length;
if(result_count > 0){
//block a;
}else if(s_limit==0){
//block b;
}else{
//block c;
}
}});
我使用PHP作爲後端。這段代碼可以在我的本地服務器上正常工作,並可以在使用下面的json的實時服
{"root": {"success":"1","message":"Successfully retrieved data.","data":{"partners":[{"store_name":"Mega Mart (Readymade Brands)","store_address":"Next to SBI, Vyttila, Ernakulam","store_phone":"","item_name":"Festival of Young at 999","item_description":"Megamart celebrates the spirit of being young. Take home 4 groovy T-shirts or 2 stylish shirts or 3 women kurtas for just rupees 999.","item_offer":"999 Offer","offer_expiry":"2014-06-08","tag1":"T-shirt","tag2":"Dress","tag3":"Jeans","store_id":"a9e12c46-ee00-11e3-a5e4-bc305be6e93e"}]}}}
但對於此JSON,
{"root": {"success":"2","message":"no results found","data":{"partners":[]}}}
在它顯示了直播服務器
,
SyntaxError: JSON.parse: unexpected character
var dataArray = JSON.parse(data);
我試圖從我的代碼刪除JSON.parse但它表明
TypeError: dataArray.root is undefined
var array_locations=dataArray.root.data.locations;
請幫我找到解決方案。 謝謝。
您的搜索數據與數據庫數據不符。這就是爲什麼在''消息'中沒有找到結果' – Ranjith
@Renjith:那很好,但是錯誤是什麼JSON.parse:意外字符 – Jobz