我做一個Ajax請求的方式如下:錯誤的字符集在JSONP AJAX請求
function apiCall(resource, data, callback) {
if(data == undefined || data == null) data = {};
$.ajax({
dataType: 'jsonp',
data: data,
url: nodeUri + "/" + resource
}).success(callback).error(function(jqXHR, textStatus, errorThrown) { alert(errorThrown); });
}
儘管這適用於大多數呼叫,有些JSONP請求失敗顯示錯誤「未結束的字符串」在螢火控制檯這個字符串:
jQuery151005229747375124583_1303069060...size chart women)\r\n\r\nx-small \r\n
當我複製請求主體和console.log
更換jQuery的回調函數的名稱,它失敗了同樣的錯誤,但如果我把它複製到記事本++在ANSI模式下張貼回控制檯前,它工作正常。響應頭中的字符集是utf-8。
jquery解析這些請求失敗的原因是什麼?
編輯:我用express作爲服務器使用node.js。
這是整個響應體我從Ajax請求得到:
jQuery151019804588618633323_1303079165535({"price":{"currency_code":"EUR","cents":12000},"image_colors":[{"blue":99,"hex":"636363","green":99,"red":99},{"blue":255,"hex":"ffffff","green":255,"red":255}],"default_image":{"big":"http://s32.dawandastatic.com/Product/9709/9709590/big/1271709158-113.jpg?20101021091023","mini":"http://s32.dawandastatic.com/Product/9709/9709590/mini/1271709158-113.jpg?20101021091023","listview":"http://s32.dawandastatic.com/Product/9709/9709590/listview/1271709158-113.jpg?20101215201220","thumb":"http://s32.dawandastatic.com/Product/9709/9709590/thumb/1271709158-113.jpg?20101021091023","listing":"http://s32.dawandastatic.com/Product/9709/9709590/listing/1271709158-113.jpg?20101021091023","full":"http://s32.dawandastatic.com/Product/9709/9709590/full/1271709158-113.jpg?20101021091023","long":"http://s32.dawandastatic.com/Product/9709/9709590/long/1271709158-113.jpg?20101021091023","pin":"http://s32.dawandastatic.com/Product/9709/9709590/pin/1271709158-113.jpg?20101215201220"},"user":{"name":"goodmorningmidnight","id":1791458,"restful_path":"https://stackoverflow.com/users/1791458"},"ranking":0.6636759628828178,"likes":0,"dislikes":0,"_id":"4da23faa2264ef7b9defbdac","base_price":null,"category":{"name":"1031","id":530,"restful_path":"/categories/530"},"category_id":530,"created_at":null,"description":"Rock/grau, weiß\r\n\r\n[ m a t e r i a l ]\r\n100 % wolle (leichte sommerwolle)\r\n\r\n[ d e t a i l s ]\r\nasymmetrisch\r\npatchwork design\r\nlagenlook\r\n\r\n[ s i z e s ]\r\nXS, S, M, L \r\n\r\n[ c a r e ]\r\nschonwäsche 30°C, mit ähnlichen farben waschen, nicht bleichen, nicht im wäschetrockner trocknen, bügeln mit mittlerer stufe\r\n\r\n[ s i z e c h a r t ]\r\n\r\n(size chart women)\r\n\r\nx-small \r\n
brust: (76-80 cm)
\r\ntaille: (60-64 cm)
\r\nhüfte: (84-88 cm)\r\n\r\nsmall
\r\nbrust: (84-88 cm)
\r\ntaille: (68-72 cm)
\r\nhüfte: (92-96 cm)\r\n\r\nmedium
\r\nbrust: (92-96 cm)
\r\ntaille: (76-80 cm)
\r\nhüfte: (100-104 cm)\r\n\r\nlarge
\r\nbrust: (100-104 cm)
\r\ntaille: (84-88 cm)
\r\nhüfte: (108-112 cm)\r\n","ending":null,"id":9709590,"materials":"","name":"1.01 (all your summer songs)","product_url":"http://de.dawanda.com/product/9709590-101-all-your-summer-songs","quantity":1,"restful_path":"/products/9709590","tags":""});
我覺得JSON是確定的,如果你將其轉換爲ANSI,但似乎是UTF-8這樣的破壞。
該對象在通過Mongoose從MongoDB加載後,在服務器端用JSON.stringify進行了字符串化。
json請求失敗的示例:http://like-my-style.com/#!single/9709590。它適用於其他產品。
這似乎來自服務器端。你用什麼來生成jsonp響應? – 2011-04-17 20:25:14
我使用node.js – Thomas 2011-04-17 20:36:56
我注意到這個字符串:'hüfte' - 這可能是問題嗎? – Hogan 2011-04-17 22:36:47