Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

jQuery ajax usage method


May 30, 2021 Article blog


Table of contents


How ajax is used

type Default: "GET"). How to request ("POST" or "GET"), defaulting to "GET"

url Default: Current page address. The address at which the request was sent.

data The data sent to the server. A utomatically converts to the request string format. T he GET request will be attached after 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 that the server is expected to return. If not specified, jQuery will automatically be intelligently judged based on HTTP package MIME information

success The callback function after a successful request. Argument: Data returned by the server and processed according to the dataType parameter;

1. Pass the specified parameters

$.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 the data processing and returns the results

@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;
}

4. The result returned is output .log console

 jQuery ajax usage method1