jsonp 請求報Uncaught SyntaxError: Unexpected token :


最簡單的Jsnop請求 確報Uncaught SyntaxError: Unexpected token :

最後問了一下google,才明白$(document).ready(function() { jQuery.ajax({ type: 'GET', url: 'http://wncrunners.com/admin/colors.json' , dataType: 'jsonp', success: function(data) { alert('success'); } }); });//end document.ready

You've told jQuery to expect a JSONP response, which is why jQuery has added the callback=jQuery16406345664265099913_1319854793396&_=1319854793399 part to the URL (you can see this in your dump of the request).ajax

What you're returning is JSON, not JSONP. Your response looks likejson

{"red" : "#f00"}

and jQuery is expecting something like this:app

jQuery16406345664265099913_1319854793396({"red" : "#f00"})

If you actually need to use JSONP to get around the same origin policy, then the server serving colors.json needs to be able to actually return a JSONP response.jsonp

If the same origin policy isn't an issue for your application, then you just need to fix the dataType in your jQuery.ajax call to be json instead of jsonp.this

相關文章
相關標籤/搜索