Home > Article > Web Front-end > How to implement Ajax to send a request without success
This time I will show you how to implement Ajax to send requests but not enter success, and how to implement Ajax to send requests but not enter success. What are the precautions? The following is a practical case, let's take a look.
1. Situation description: ajax was sent successfully, the background also successfully responded to the request, and returned json data. You can also see the response json by monitoring the request through chrome The data, but does not enter the success method, instead goes to 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' }); } });
After 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"; }
Cause: When the json data returned by the background is a pure String type object, the front end After the dataTypeattribute is set to json, it will be considered that the json data format converted from the String object is not the standard json format, so the method corresponding to the error will be 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 cases: When the json number returned by the backend is similar to "1", "2", "22", "232123", "-1", "232123.44" digital characters When string , when the front-end dataType attribute is set to json, the success method can be entered normally. The reason is unknown.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:Using Ajax and forms to implement the functions required for registered users
Steps to implement WeChat web page authorized login using ajax (With code)
The above is the detailed content of How to implement Ajax to send a request without success. For more information, please follow other related articles on the PHP Chinese website!