2011-10-17 69 views
0

通過Ajax我收到來自PHP腳本編碼的JSON字符串(json_encode):報價在jQuery.parseJSON

response = "{"type":"ok","mess":"File successfuly uploaded"}" 

,當我試圖解析這個字符串jQuery.parseJSON(response); JS腳本失敗堂妹在啓動雙引號並最終。

正常工作:

jQuery.parseJSON('{"type":"ok","mess":"File successfuly uploaded"}'); 

如何解決這個問題呢?

我總是收到語法錯誤: 「UNEXPECTED_TOKEN」

解決:原因是不正確的文件編碼。 UTF-8沒問題

+0

試圖逃逸引號` 「{\」 類型\」 ...` – 2011-10-17 12:19:45

回答

2

你已經解決了這個問題。您不能創建JSON字符串並忽略引用嵌套的標準規則。無論是其中的一個將工作:

//Denote string by using single quotes 
response = '{"type":"ok","mess":"File successfully uploaded"}' 

OR

//Continue to use double quotes and escape the inner quotes 
response = "{\"type\":\"ok\",\"mess\":\"File successfully uploaded\"}"; 
0

如何:

response = "{\"type\":\"ok\",\"mess\":\"File successfuly uploaded\"}"; 
response = "'" + response + "'"; 
jQuery.parseJSON(response);