Home > Web Front-end > JS Tutorial > body text

Solution to Ajax request being sent successfully but not succeeding (picture and text tutorial)

亚连
Release: 2018-05-21 16:28:38
Original
3004 people have browsed it

Now I will share 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.

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'
   });
 }
});
Copy after login

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";
}
Copy after login

Reason: When the json data returned by the background is a pure String type object, after the front-end dataType attribute is set to json, it will be considered that the json data format converted from the String object is not a 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'
     });
   }
});
Copy after login

Special cases: When the json number returned by the backend is similar to "1", "2", "22", "232123", " -1", "232123.44" numeric string, when the front-end dataType attribute is set to json, the success method can be entered normally. The reason is unknown.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Struts2 and Ajax data interaction (graphic tutorial)

Pull-up loading written in native ajax Example (graphic tutorial)

MUi framework ajax request WebService interface example_AJAX related

The above is the detailed content of Solution to Ajax request being sent successfully but not succeeding (picture and text tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template