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

How to implement Ajax to send a request without success

php中世界最好的语言
Release: 2018-03-30 15:30:44
Original
1033 people have browsed it

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

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

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

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!

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