Detailed explanation of the parsererror error case in JavaScript solution in ajax

Detailed explanation of the parsererror error case in JavaScript solution in ajax

The ultimate way to solve the parsererror error of ajax (the problem of json data transmitted from the background to the front end)

The reason for this problem is that there is a problem with the data transmitted from the background to the front end. Ajax is particularly strict about the format of JSON.

The following is the ajax request that will cause this problem

$.ajax({
type:'get',
url:"{php echo $this->createWebUrl('ajax',array('ac'=>'cunByXiangId'))}",
data:{id:id},

  dataType:'json', //This is the key to the problem success:function(obj){

},error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});

The main problem lies in the line of code dataType:'json', which means that the received data is in json format. At this time, we only need to delete this code and we will receive the string format.

The data is then converted into json format. The following is the code

$.ajax({
type:'get',
url:"{php echo $this->createWebUrl('ajax',array('ac'=>'cunByXiangId'))}",
data:{id:id},
success:function(obj){
obj=eval('('+obj+')');//This code converts the string into json format},error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});

This is the end of this article about JavaScript solving parsererror errors in ajax. For more relevant JavaScript solving parsererror errors in ajax, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Ajax solves cross-domain problem by setting CORS response header to achieve cross-domain case study
  • Three-level linkage provincial and municipal ajax code
  • JSON, AJAX, Maven Basics
  • Ajax login verification implementation code
  • Ajax realizes the linkage between provinces, cities and districts
  • jQuery+Ajax to achieve simple paging effect
  • Preliminary implementation of Ajax (using vscode+node.js+express framework)
  • How to use AJAX to obtain Django backend data

<<:  How to open ports to the outside world in Alibaba Cloud Centos7.X

>>:  Detailed example of using case statement in MySQL stored procedure

Recommend

CentOS uses local yum source to build LAMP environment graphic tutorial

This article describes how to use the local yum s...

How to encapsulate query components based on element-ui step by step

Table of contents Function Basic query functions ...

Detailed explanation of the use of bus in Vue

Vue bus mechanism (bus) In addition to using vuex...

Vue Virtual DOM Quick Start

Table of contents Virtual DOM What is virtual dom...

Detailed explanation of efficient MySQL paging

Preface Usually, a "paging" strategy is...

Display and hide HTML elements through display or visibility

Sometimes we need to control whether HTML elements...

Example of exporting and importing Docker containers

Table of contents Exporting Docker containers Imp...

Share 13 excellent web wireframe design and production tools

When you start working on a project, it’s importa...

MySQL database backup and recovery implementation code

Database backup #grammar: # mysqldump -h server-u...

View the frequently used SQL statements in MySQL (detailed explanation)

#mysql -uroot -p Enter password mysql> show fu...

Detailed explanation of the problem of CSS class names

The following CSS class names starting with a num...

Detailed explanation on reasonable settings of MySQL sql_mode

Reasonable setting of MySQL sql_mode sql_mode is ...

React+Amap obtains latitude and longitude in real time and locates the address

Table of contents 1. Initialize the map 2. Map Po...

vue+echarts realizes the flow effect of China map (detailed steps)

@vue+echarts realizes the flow effect of China ma...

HTML implementation of a simple calculator with detailed ideas

Copy code The code is as follows: <!DOCTYPE ht...