Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways to use Ajax in Jquery?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "what are the ways to use Ajax of Jquery", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the use of Ajax in Jquery?" this article can help you solve your doubts.

Ajax Technology of Jquery (key points)

Jquery is an excellent js framework, which naturally encapsulates the native ajax of js. The operation method of the encapsulated ajax is more concise and more powerful. There are the following jquery methods related to ajax operation, but three are often used in development:

1) $.get (url, [data], [callback], [type])

2) $.post (url, [data], [callback], [type])

Difference: get has bytecode garbled problem, post has no bytecode garbled problem (getting request page data to the background, background response.setContextType ("text/html;charset=UTF-8") response garbled problem remains the same as before)

Where:

Url: represents the requested server-side address

Data: represents the data on the request server side (either in key=value or json format)

Callback: indicates that the function triggered by a successful response on the server side (executed only when a normal successful response is returned)

Type: indicates the data type returned by the server (jquery automatically converts the type according to the specified type) commonly used return types: text, json, html, etc.

If type is json, and the json format string returned by the server will be converted automatically by jq, the argument obtained in the callback function is the converted js object and can be used directly.

The order of parameters can be changed, but if you leave the data later, you will have the problem of not passing data, so don't change it without authorization and use it strictly in the order in the jq document.

Function get () {

$.get (

"/ web22-ajax/ajaxServlet2", / / url

{"name": "muzidigbig", "age": 22}, / / request parameters, data format of json

Function (data) {/ / parameters returned after a successful request

Alert (data.name+data.age)

}

"json"

)

}

/ / java can only be java code, and the code of front-end pages can only be converted into json objects.

Response.getWriter () .write ("{\" name\ ":\" muzi\ ",\" age\ ": 22}")

3) $.ajax ({option1:value1,option2:value2... ); (important)

The commonly used option is as follows:

Async: whether asynchronous. Default is true, which stands for async.

Url: request server-side address

Type: request method, POST/GET (default get is not written)

Data: the parameter sent to the server. Json format is recommended.

Success: the function executed in response successfully. The corresponding type is function. The value obtained after a successful request is automatically encapsulated in the first parameter of this function.

Error: the function executed by the failed response, corresponding to the type function

DataType: the type of data returned by the server, commonly used text and json

BeforeSend:function (argument) {}, / / is called before sending the request. You can do some verification and other processing. If false is returned, you can cancel the ajax request.

Send a request

$('button') .click (function () {

$.ajax ({

Async:true

Url: "send request to background address"

The method of type:'post',// request

Data: {'name':'muzidigbig','age':'20'}, / / requested data

The data returned by success:function (backdata) {/ / after a successful request will be encapsulated in the first parameter of the callback function.

/ / obtain the required data through backdata

Alert (backdata.name+backdata.age)

}

Error:function () {/ / function returned when the response is unsuccessful

Console.log ('request failed!')

}

DataType:'json'// sets the type of data returned

})

})

After reading this, the article "what is the use of Ajax in Jquery" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report