Three ways asynchronous request ajax

Ajax can without having to load the entire page to update part of the page content, you can reduce the waste of server resources.

There are four general ajax implementation, since implementations based JS too complex, with substantially less, so that it is temporarily put the implementation code.

1. $ .ajax () send an asynchronous request

Parameter list $ .ajax as follows:

url: Path asynchronous request.

type: Request mode, common parameters for the post, get and so on.

dataTpye: format of the received server response data.

data: request parameters, typically sent in json manner.

success: function (data): After a successful callback response, the parameter data is typically data returned by the server json format.

error: function (): case of errors in response to the request, the execution of the callback function.

$.ajax({
    
                        url:"/login.do" , 
                        type:"POST" , 
                        dataType:"text",
                    data:{"name":"zhangbo"},
                    success:function (data) {
                        alert(data);
                    },
                    error:function () {
                        alert("请求响应错误");
                    }
                });                            

2. $ .get () get request sent

$ .Get (url, [data], [callback], [type]) the following list of parameters:

url: Path asynchronous request.

data: request parameters, typically sent in json manner.

callback: callback function.

type: type of response results.

$.get("/login.do",{username:"zhangbo"},function (data) {
                alert(data);
            },"text");

3. $ .post () request sent post

$ .Post (url, [data], [callback], [type]) the following list of parameters:

url: Path asynchronous request.

data: request parameters, typically sent in json manner.

callback: callback function.

type: type of response results.

$.post("/login.do",{username:"zhangbo"},function (data) {
              alert(data);
            },"text");

Asynchronous implementation of three ways are similar, the key lies in obtaining the use of ajax with json response format of the data, the other is not difficult.

Guess you like

Origin www.cnblogs.com/Code-Handling/p/11926476.html