Javascript JSON解析错误,即使请求成功
即使服务器返回成功,我也会收到一个JSON解析错误。下面是我的保存代码,其中fail()总是在运行Javascript JSON解析错误,即使请求成功,javascript,jquery,backbone.js,http-headers,coffeescript,Javascript,Jquery,Backbone.js,Http Headers,Coffeescript,即使服务器返回成功,我也会收到一个JSON解析错误。下面是我的保存代码,其中fail()总是在运行 @model.save() .fail(=> @resetForm() ) .always (obj, error) -> console.log obj console.log obj.responseText console.log JSON.parse(obj.responseText) 这是我的错误对象: "parsererror" "No c
@model.save()
.fail(=> @resetForm() )
.always (obj, error) ->
console.log obj
console.log obj.responseText
console.log JSON.parse(obj.responseText)
这是我的错误对象:
"parsererror"
"No conversion from text to http://api2.local/users/auth"
一些注意事项:
我使用Jquery 1.8.3和主干网0.9.9服务器使用json正确响应-这是我的响应头
Access-Control-Allow-Headers:origin, x-requested-with, content-type, accept
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Max-Age:86400
Connection:Keep-Alive
Content-Length:202
Content-Type:application/json; charset=utf-8
Date:Fri, 21 Dec 2012 18:46:25 GMT
Keep-Alive:timeout=5, max=100
Server: xxx
X-Powered-By:PHP/5.3.1
console.log JSON.parse(obj.responseText)正确地为我提供了一个JSON对象
编辑:请求头
编辑:通过POST请求有效负载
编辑:响应有效负载
啊-编程有时会很烦人。终于明白了——谢谢大家的帮助,但这只是一个简单的咖啡脚本编译问题 所以在我之前
$.ajaxPrefilter ( (options, originalOptions, jqXHR) ->
options.url = "#{ API_URL }" + options.url
)
编译以返回options.url和$.ajaxPrefilter。无论出于何种原因,Jquery都会将options.url作为ajaxPrefilter函数的一部分。解决方案是返回false:
$.ajaxPrefilter \
(options, originalOptions, jqXHR) ->
options.url = "#{ API_URL }" + options.url
no
它给出了正确的编译版本
return $.ajaxPrefilter(function(options, originalOptions, jqXHR) {
options.url = ("" + API_URL) + options.url;
return false;
});
请求是什么样子的?特别是标题,…如果您得到的是正确的JSON对象,那么这显然不是JSON解析错误。上面添加了请求标题-是的,解析正确,这就是为什么我如此困惑的原因。您认为这可能是授权/登录问题吗?您是否使用服务器进行了身份验证?这不是JSON解析错误,该错误来自jQuery的自动数据类型转换。看,哇,我已经为同样的问题绞尽脑汁好几个小时了,谢谢
$.ajaxPrefilter ( (options, originalOptions, jqXHR) ->
options.url = "#{ API_URL }" + options.url
)
$.ajaxPrefilter \
(options, originalOptions, jqXHR) ->
options.url = "#{ API_URL }" + options.url
no
return $.ajaxPrefilter(function(options, originalOptions, jqXHR) {
options.url = ("" + API_URL) + options.url;
return false;
});