我正在通过以下方式执行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请求在使用此字符串的firebug控制台中显示错误“unterminated string literal”时失败:
jQuery151005229747375124583_1303069060...size chart women)\r\n\r\nx-small \r\n
当我复制请求主体并用console.log
替换jquery回调函数名时,它失败并出现相同的错误,但是如果我将它复制到ANSI模式的Notepad ++,然后再将其发送回控制台,它就能工作精细。响应头中的字符集是utf-8。
jquery解析这些请求失败的原因是什么?
编辑:我使用node.js和express作为服务器。
这是我从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":"/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,JSON就可以了,但似乎在utf-8中就这样打破了。
通过Mongoose从MongoDB加载后,对象在服务器端用JSON.stringify进行字符串化。
json请求失败的示例:http://like-my-style.com/#!single/9709590。它适用于其他产品。
答案 0 :(得分:2)
if(data == undefined || data == null) data = {};
data == null
检查值null
和undefined
,因此您只需要检查一个而不是两者。
至于您的实际问题,您的节点代码似乎没有创建正确的JSON数据。尝试使用JSON.stringify
创建您的回复,这样您就可以确信它是有效的json。
您的数据的一部分是“weiß”尝试从描述中删除所有非标准字符,看看是否会导致问题消失。