This article mainly shares with you a solution to the problem that the Ajax request is sent successfully but does not enter the success. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.
1. Situation description: Ajax is sent successfully, the background also successfully responds to the request, and returns json data. You can also see the response json data by monitoring the request through chrome, but the success method is not entered. Instead, it ran into the 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("进入了error方法",{ skin: 'layui-layer-molv' }); } });
Back-end:
@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 by the background is a pure String type object, the front-end dataType After the attribute is set to json, it will be considered that the json data format converted by the String object is not the standard json format, so the method corresponding to the error is executed.
Solution: No need to change the backend, 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("进入了error方法",{ skin: 'layui-layer-molv' }); } });
Special case: When the json number returned by the backend is similar to "1" , "2", "22", "232123", "-1", "232123.44" When the numeric string is used, the success method can be entered normally when the front-end dataType attribute is set to json. The reason is unknown.
Related recommendations:
A brief analysis of the problem of json data transmitted from Ajax background success
ThinkPHP page jump success and error method overview _PHP tutorial
The difference between success and complete in jQuery.ajax
The above is the detailed content of How to solve the problem of Ajax request being sent successfully but not succeeding. For more information, please follow other related articles on the PHP Chinese website!