“parsererror” form ajax request

Posted by

Here i am  getting a “parsererror” from jquery for an Ajax request,

This ajax code

this.LoadViewContentNames = function () { $.ajax({ url: ‘/Admin/Ajax/GetViewContentNames’, type: ‘POST’, dataType: ‘json’, data: { viewID: $(“#view”).val() }, success: function (data) { alert(data); }, error: function (data) { debugger; alert(“Error”); } }); };

jquery fires the error event for the $.ajax() method saying “parsererror”.

Solution:

After doing some research i get my answer

I have specified the ajax call response dataType as:

‘json’

The actual ajax response is not a valid JSON and as a result the JSON parser is throwing an error.

You have specified the ajax call response dataType as:

‘json’

where as the actual ajax response is not a valid JSON and as a result the JSON parser is throwing an error.

So, The best approach that I would recommend is to change the dataType to:

‘text’

$.ajax({ url: ‘/Admin/Ajax/GetViewContentNames’, type: ‘POST’, dataType: ‘text’, data: {viewID: $(“#view”).val()}, success: function (data) { try { var output = JSON.parse(data); alert(output); } catch (e) { alert(“Output is not valid JSON: ” + data); } }, error: function (request, error) { alert(“AJAX Call Error: ” + error); } });
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x