Ajax Request Sending Successful but Not Successful Solution

Time:2019-7-18

1. Description of the situation:Ajax sent successfully, the background also successfully responded to the request, and returned JSON data, through the chrome monitoring request can also see the response of JSON data, but instead of success method, it ran to error method.

Front end:

$.ajax({
 type : "get",
 data : {'dbId':node.dbId,'viewId':node.id,'date':new Date()},
 url : "${ctp}/ViewOperate/ShowViewSql",
 dataType : "json",
 success : function(data){
   console.log(data);
   layer.alert(data,{
     skin: 'layui-layer-molv'
   });
 }
 error : function(data){
   Layer. alert ("Enter the error method"),{
     skin: 'layui-layer-molv'
   });
 }
});

Backend:


@RequestMapping(value="/ShowViewSql",method=RequestMethod.GET)
@ResponseBody
public String showCreateViewSql(@RequestParam(value="dbId",required=false)Integer dbId,
   @RequestParam(value="viewId",required=false)Integer viewId) {
 return "abc";
}

Reason:When the JSON data returned from the background is a pure String type object, when the front-end dataType attribute is set to json, the JSON data format converted by the String object is not the standard JSON format, so the error corresponding method is executed.

Solve:The back end does not need to be changed, just set the dataType attribute in the front-end Ajax request to text.

$.ajax({
   type : "get",
   data : {'dbId':node.dbId,'viewId':node.id,'date':new Date()},
   url : "${ctp}/ViewOperate/ShowViewSql",
   dataType : "text",
   success : function(data){
     console.log(data);
     layer.alert(data,{
       skin: 'layui-layer-molv'
     });
   }
   error : function(data){
     Layer. alert ("Enter the error method"),{
       skin: 'layui-layer-molv'
     });
   }
});

Exceptional case:When the number of JSON returned by the back end is similar to the number string of “1”, “2”, “22”, “232123”, “1”, “232123.44”, the front-end dataType property is set to json, it can also enter the success method normally, for unknown reasons.

The above Ajax request to send a successful but not progressive success solution is the editor to share with you all the content, I hope to give you a reference, but also hope that you support developpaer more.