How to use ajax in jQuery
This article mainly introduces how to use ajax in jQuery, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
How ajax is used
Type: default value: "GET"). Request method ("POST" or "GET"), default is "GET"
Url: default value: current page address. The address from which the request was sent.
Data: data sent to the server. Will be automatically converted to the request string format. The GET request will be appended to the URL. Must be in Key/Value format
ContentType: the type of content encoding when sending information to the server.
DataType: the type of data expected to be returned by the server. If not specified, jQuery will automatically judge intelligently based on the MIME information of the HTTP package
Success: the callback function after a successful request. Parameters: data returned by the server and processed according to the dataType parameter; a string describing the status.
1. Pass the specified parameter $.ajax ({
Type: 'post'
Url:'/ operate/getNewsBynew_id'
Data: {"new_id": data.new_id}
Success: function (data) {
}
); 2. Pass the json object $.ajax ({
Type: 'post'
Url:'/ news/addnews'
ContentType: "application/json; charset=utf-8"
Data: JSON.stringify (data.field)
Success: function (data) {
Location.href= "/ gover_index_1.html"
}
}
); 3. The background accepts data processing and returns the result @ PostMapping ("/ addnews")
Public News addNews (@ RequestBody News news) {
News.setTime (new Date ())
NewsService.insertNews (news)
Return news
} @ PostMapping ("/ getNewsBynew_id")
Public NewsAll getNews (Integer new_id) {
NewsAll newsAll=newsService.getNewsBynew_id (new_id)
System.out.println (newsAll.getS_times () + "+ newsAll.getTitle ())
Return newsAll
} Thank you for reading this article carefully. I hope the article "how to use ajax in jQuery" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!