SoFunction
Updated on 2025-03-03

JavaScript to solve parsererror error case in ajax

The ultimate solution to ajax parsererror error (the data json problem transmitted to the front desk)

The reason for this problem is that there is a problem with the data transmitted to the front desk in the background. Ajax is particularly strict with the json format.

Below 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 place is the key to the problem
success:function(obj){

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

The main problem lies in dataType:'json', this line of code, which means that the data received and returned 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 to 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 strings into json format},error: function(XMLHttpRequest, textStatus, errorThrown) {
alert();
alert();
alert(textStatus);
}
});

This is the article about JavaScript's detailed explanation of parsererror error cases in ajax. For more related JavaScript to solve parsererror errors in ajax, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!