jQuery returning parsererror for ajax request
I recently encountered this problem and stumbled upon this question.
I resolved it with a much easier way.
Method One
You can either remove the dataType: 'json'
property from the object literal...
Method Two
Or you can do what @Sagiv was saying by returning your data as Json
.
The reason why this
parsererror
message occurs is that when you simply return a string or another value, it is not really Json
, so the parser fails when parsing it.So if you remove the dataType: json
property, it will not try to parse it as Json
.
With the other method if you make sure to return your data as Json
, the parser will know how to handle it properly.
Ajax post call returns parsererror
The problem is with the dataType mentioned in the ajax call.
The post method is not returning any json data,
by changing the dataType :'json' to dataType:'text' fixed the issue.
Thanks Jaromanda X and Mathew Jibin for your inputs
jQuery Ajax returns parsererror despite JSON is valid and return code is 201
Be careful: ResponseText is equal to "" and that is not a valid json. It should be null or "{}"
jquery ajax request with json throwing parsererror
Replace your line
data: mydata,
withdata: JSON.stringify(mydata),
contentType: "application/json",
jQuery.ajax() parsererror
It's because you're telling jQuery that you're expecting JSON-P, not JSON, back. But the return is JSON. JSON-P is horribly mis-named, named in a way that causes no end of confusion. It's a convention for conveying data to a function via a script
tag. In contrast, JSON is a data format.
Example of JSON:
{"foo": "bar"}
Example of JSON-P:yourCallback({"foo": "bar"});
JSON-P works because JSON is a subset of JavaScript literal notation. JSON-P is nothing more than a promise that if you tell the service you're calling what function name to call back (usually by putting a callback
parameter in the request), the response will be in the form of functionname(data)
, where data
will be "JSON" (or more usually, a JavaScript literal, which may not be the quite the same thing). You're meant to use a JSON-P URL in a script
tag's src
(which jQuery does for you), to get around the Same Origin Policy which prevents ajax requests from requesting data from origins other than the document they originate in (unless the server supports CORS and your browser does as well). I can't solve this parsererror ajax error
In short you are returning a string of text from your django view which is not a valid JSON string; therefore your javascript cannot parse it as "valid" json.
The offending line is return HttpResponse("ajax is done!")
. Instead change this to return json.
return HttpResponse(json.dumps({'status': 'ajax is done!'}))
Or, modify your jquery to handle html. This an be done by changing the line: 'dataType': 'json',
to be 'dataType': 'html',
. Ajax call returns parsererror
Actually it seems there is a bug in jquery 1.5.1 and 1.5.0 (which does not exist in previous versions).
In fact when you use dataType: 'json'
it tries to parse json as a script and you should use dataType: 'text/json'
instead. see this link.
Related Topics
Domnodeinserted Equivalent in Ie
How to Convert a Firestore Date/Timestamp to a Js Date()
How to Add a "Readonly" Attribute to an <Input>
How to Call a Dynamically-Named Method in JavaScript
How to Create a Date Object from String in JavaScript
How to Pass Data from Child to Parent Component Angular
Jquery Convert Line Breaks to Br (Nl2Br Equivalent)
Properties of JavaScript Function Objects
What's the Purpose of Starting Semi Colon at Beginning of JavaScript
Add Commas to a Number in Jquery
Read a Text File Using Node.Js
Get Width Height of Remote Image from Url
How Does Internationalization Work in JavaScript
How Do Cors and Access-Control-Allow-Headers Work
How to Update a Specific Index from the Array in Firestore
Dynamic Require in Requirejs, Getting "Module Name Has Not Been Loaded Yet for Context" Error