简体   繁体   中英

JSON ParseError even though request was successful

I am getting a JSON parseerror even though the return from the server was successful. Here is my save code where fail() is always being run

@model.save()
  .fail(=> @resetForm() )
  .always (obj, error) ->
    console.log obj
    console.log obj.responseText
    console.log JSON.parse(obj.responseText)

Here is my error object:

"parsererror"
"No conversion from text to http://api2.local/users/auth"

Some notes:

I am on Jquery 1.8.3 and Backbone 0.9.9
The server responds correctly with json - here is my response header

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) correctly gives me a JSON object

EDIT: Request Headers

Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:54
Content-Type:application/json
Host:api2.local
Origin:http://localhost:3000
Pragma:no-cache
Referer:http://localhost:3000/login
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.101 Safari/537.11
X-Requested-With:XMLHttpRequest

EDIT: Request Payload via POST

{"email":"x@x.com","password":"xxx"}

EDIT: Response Payload

{
"user_id":"xx",
"first_name":"xxx",
"last_name":"xxx",
"email":"x@x.com",
"role":"xxxx",
"date_joined":"xxx"
}

Ahhhh - Programming can be so irritating sometimes. Finally figured this out - thanks for everyones help but it was a simple coffeescript compiling issue

So before I had

$.ajaxPrefilter ( (options, originalOptions, jqXHR) -> 
 options.url = "#{ API_URL }" + options.url
)

which compile to return both options.url and $.ajaxPrefilter. For whatever reason, Jquery was picking up the options.url as a DataTypes argument on the ajaxPrefilter function. The solution was to return false:

$.ajaxPrefilter \
  (options, originalOptions, jqXHR) -> 
    options.url = "#{ API_URL }" + options.url
    no 

which gives the correct compiled version

return $.ajaxPrefilter(function(options, originalOptions, jqXHR) {
  options.url = ("" + API_URL) + options.url;
  return false;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM