I am doing an ajax request the following way:
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); });
}
While this works for most calls, some jsonp requests fail showing the error "unterminated string literal" in the firebug console with this string:
jQuery151005229747375124583_1303069060...size chart women)\r\n\r\nx-small \r\n
When I copy the request body and replace the jquery callback function name with console.log
, it fails with the same error, but if I copy it to Notepad++ in ANSI mode before posting it back to the console, it works fine. The charset in the response header is utf-8.
What could be the reason for jquery to fail parsing these requests?
Edit: I am using node.js with express as server.
This is the whole response body I am getting from the ajax request:
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)\开发者_JS百科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":""});
I think the JSON is ok if you convert it to ANSI, but seems to be broken like this in utf-8.
The object is stringified on the server side with JSON.stringify after being loaded from MongoDB via Mongoose.
An example where the json request fails: http://like-my-style.com/#!single/9709590 . It works fine for other products.
if(data == undefined || data == null) data = {};
data == null
checks for both the value null
and undefined
so you only need to check for one not both.
As for your actual issue it seems that your node code is not creating proper JSON data. try using JSON.stringify
to create your response so you have confidence that it's valid json.
A subset of your data is "weiß" try removing all non standard characters from your description and see if that makes the problem go away.
精彩评论