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

MySQL 5.7.17 installation and configuration method graphic tutorial (windows)

1. Download the software 1. Go to the MySQL offic...

Detailed tutorial on installing nacos in docker and configuring the database

Environment Preparation Docker environment MySQL ...

What are the attributes of the JSscript tag

What are the attributes of the JS script tag: cha...

How to install babel using npm in vscode

Preface The previous article introduced the insta...

JavaScript ECharts Usage Explanation

I used ECharts when doing a project before. Today...

Tutorial on installing rabbitmq using yum on centos8

Enter the /etc/yum.repos.d/ folder Create rabbitm...

Install Tomcat on Linux system and configure Service startup and shutdown

Configure service startup and shutdown in Linux s...

Implementing user registration function with js

This article example shares the specific code of ...

Detailed steps for smooth transition from MySQL to MariaDB

1. Introduction to MariaDB and MySQL 1. Introduct...

How to update, package, and upload Docker containers to Alibaba Cloud

This time, we will try to package the running con...

Detailed explanation of views in MySQL

view: Views in MySQL have many similarities with ...

Detailed explanation of keepAlive usage in Vue front-end development

Table of contents Preface keep-avlive hook functi...

React Hooks Detailed Explanation

Table of contents What are hooks? Class Component...

Building a Redis cluster on Docker

Table of contents 1. Pull the image 2. Create a R...