{"id":530,"date":"2023-05-26T05:16:27","date_gmt":"2023-05-26T05:16:27","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=530"},"modified":"2023-05-26T05:17:32","modified_gmt":"2023-05-26T05:17:32","slug":"parsererror-form-ajax-request","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/parsererror-form-ajax-request\/","title":{"rendered":"&#8220;parsererror&#8221; form ajax request"},"content":{"rendered":"\n<p>Here i am \u00a0getting a &#8220;parsererror&#8221; from jquery for an Ajax request,<\/p>\n\n\n\n<p>This ajax code<\/p>\n\n\n\nthis.LoadViewContentNames = function () {\n    $.ajax({\n        url: &#8216;\/Admin\/Ajax\/GetViewContentNames&#8217;,\n        type: &#8216;POST&#8217;,\n        dataType: &#8216;json&#8217;,\n        data: { viewID: $(&#8220;#view&#8221;).val() },\n        success: function (data) {\n            alert(data);\n        },\n        error: function (data) {\n            debugger;\n            alert(&#8220;Error&#8221;);\n        }\n    });\n};\n\n\n\n<p>jquery fires the error event for the $.ajax() method saying &#8220;parsererror&#8221;.<\/p>\n\n\n\n<p><strong>Solution<\/strong>:<\/p>\n\n\n\n<p>After doing some research i get  my answer<\/p>\n\n\n\n<p>I have specified the ajax call response\u00a0<strong>dataType<\/strong>\u00a0as:<\/p>\n\n\n\n<p><strong>&#8216;json&#8217; <\/strong><\/p>\n\n\n\n<p>The actual ajax response is not a valid JSON and as a result the JSON parser is throwing an error.<\/p>\n\n\n\n<p>You have specified the ajax call response&nbsp;<strong>dataType<\/strong>&nbsp;as:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>&#8216;json&#8217;<\/p>\n<\/blockquote>\n\n\n\n<p>where as the actual ajax response is not a valid JSON and as a result the JSON parser is throwing an error.<\/p>\n\n\n\n<p>So, The best approach that I would recommend is to change the\u00a0<strong>dataType<\/strong>\u00a0to:<\/p>\n\n\n\n<p>&#8216;text&#8217;<\/p>\n\n\n\n$.ajax({\n    url: &#8216;\/Admin\/Ajax\/GetViewContentNames&#8217;,\n    type: &#8216;POST&#8217;,\n    dataType: &#8216;text&#8217;,\n    data: {viewID: $(&#8220;#view&#8221;).val()},\n    success: function (data) {\n        try {\n            var output = JSON.parse(data);\n            alert(output);\n        } catch (e) {\n            alert(&#8220;Output is not valid JSON: &#8221; + data);\n        }\n    }, error: function (request, error) {\n        alert(&#8220;AJAX Call Error: &#8221; + error);\n    }\n});\n","protected":false},"excerpt":{"rendered":"<p>Here i am \u00a0getting a &#8220;parsererror&#8221; from jquery for an Ajax request, This ajax code this.LoadViewContentNames = function () { $.ajax({ url: &#8216;\/Admin\/Ajax\/GetViewContentNames&#8217;, type: &#8216;POST&#8217;, dataType: &#8216;json&#8217;,&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[190],"class_list":["post-530","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-parsererror-ajax-request"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=530"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/530\/revisions"}],"predecessor-version":[{"id":531,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/530\/revisions\/531"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}