Home Web Front-end JS Tutorial Solution to Ajax request being sent successfully but not succeeding (picture and text tutorial)

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

May 21, 2018 pm 04:28 PM
ajax success

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!

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

Hot Article

Hot tools Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

How to solve the 403 error encountered by jQuery AJAX request

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

How to solve jQuery AJAX request 403 error

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

How to get variables from PHP method using Ajax?

PHP vs. Ajax: Solutions for creating dynamically loaded content PHP vs. Ajax: Solutions for creating dynamically loaded content Jun 06, 2024 pm 01:12 PM

PHP vs. Ajax: Solutions for creating dynamically loaded content

How to solve the problem of jQuery AJAX error 403? How to solve the problem of jQuery AJAX error 403? Feb 23, 2024 pm 04:27 PM

How to solve the problem of jQuery AJAX error 403?

Understanding Ajax Frameworks: Explore Five Common Frameworks Understanding Ajax Frameworks: Explore Five Common Frameworks Jan 26, 2024 am 09:28 AM

Understanding Ajax Frameworks: Explore Five Common Frameworks

What are the ajax versions? What are the ajax versions? Nov 22, 2023 pm 02:00 PM

What are the ajax versions?

Asynchronous data exchange using Ajax functions Asynchronous data exchange using Ajax functions Jan 26, 2024 am 09:41 AM

Asynchronous data exchange using Ajax functions

See all articles