欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

前端給后端傳數(shù)據(jù)的五種方式總結(jié)

 更新時(shí)間:2024年07月15日 09:46:31   作者:.我非賊船  
前端與后端數(shù)據(jù)交互是軟件開(kāi)發(fā)中不可或缺的一環(huán),下面這篇文章主要給大家介紹了關(guān)于前端給后端傳數(shù)據(jù)的五種方式,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下

1.發(fā)送get請(qǐng)求將參數(shù)通過(guò)?拼接在url后面

$.ajax({
        url: "/order/userPage?page="+page+"&pageSize="+pageSize,    //請(qǐng)求的url地址  
        cache: "false",   //設(shè)置為false將不會(huì)從瀏覽器中加載請(qǐng)求信息
        async: "true",    //true所有請(qǐng)求均為異步請(qǐng)求
        dataType: "json", //請(qǐng)求返回?cái)?shù)據(jù)的格式
        type:"get",      //請(qǐng)求方式
 
上面等同于==>>
async initData(){
 
   paging: {
      page: 1,
      pageSize: 5
   }
   const res = await orderPagingApi(this.paging) 
}
 
function orderPagingApi(data) {
    return $axios({
        'url': '/order/userPage',
        'method': 'get',
        //請(qǐng)求參數(shù)
        params: {...data}
    })
 
上面等同于==>>
async initData(){
 
   paging: {
      page: 1,
      pageSize: 5
   }
   const res = await orderPagingApi(this.paging) 
}
 
function orderPagingApi(data) {
    return $axios({
 
        'url': '/order/userPage',
 
        'method': 'get',
 
        'data': data
    })
 

后端接收參數(shù)

@GetMapping("/order/userPage")
 
@ResponseBody
 
public R<Page> userPage(Integer page,Integer pageSize){}
 
或
 
@GetMapping("/order/userPage")
 
@ResponseBody
 
public R<Page> userPage(@RequestParam("page")Integer page,@RequestParam("pageSize")Integer pageSize){}

2.將參數(shù)拼接在url中,后臺(tái)通過(guò)占位符接收參數(shù) /{id}

async initData(){
    const res = await addressFindOneApi(params.id)
}
 
???????function addressFindOneApi(id) {
  return $axios({
    'url': `/addressBook/${id}`,
    'method': 'get',
  })
}

后端接收參數(shù)

@GetMapping("/addressBook/{id}")
@ResponseBody
 
public R<AddressBook> backList(@PathVariable("id")Long id){}

3.通過(guò)post提交方式將form表單中的數(shù)據(jù)序列化后傳遞到后臺(tái)。

async initData(){
    const res =await formAjax();
}
function formAjax() {
       $.ajax({
       url: "/login", 
       type: "post", 
       data: $("#form").serialize(),  // 對(duì)id為form的表單數(shù)據(jù)進(jìn)行序列化并傳遞到后臺(tái)

后端接收參數(shù)

@RequestMapping("/login")
@ResponseBody
//form表單的數(shù)據(jù)與User實(shí)體類的數(shù)據(jù)相對(duì)應(yīng)
public String login (User user) {}

4.通過(guò)post提交方式將form表單的類型是 json

async initData(){
    const res =await formAjax();
}
function formAjax() {
       $.ajax({
       url: "/login", 
       type: "post", 
       contentType: 'application/json',

后端接收參數(shù)

@RequestMapping("/login")
@ResponseBody
//form表單的數(shù)據(jù)與User實(shí)體類的數(shù)據(jù)相對(duì)應(yīng)
public String login ( @RequestBody User user) {}

5. 前臺(tái)將普通數(shù)據(jù)轉(zhuǎn)換為json

async initData(){
   paging: {
      page: 1,
      pageSize: 5
   }
   const res = await orderPagingApi(this.paging) 
}
function orderPagingApi(data) {
    return $axios({
        'url': '/order/userPage',
        'method': 'post',
         data: JSON.stringify(data),
    })

后臺(tái)接收參數(shù)

@GetMapping("/order/userPage")
@ResponseBody
public R<Page> userPage(@RequesBody Map<Integer,Integer> map){
      Integer page = map.get("page");
      Integer pageSize = map.get("pageSize");   
}
或  ==>>
//假設(shè)PageInfo類中有屬性與其相對(duì)應(yīng)
@GetMapping("/order/userPage")
@ResponseBody
public R<Page> userPage(@RequesBody PageInfo pageInfo){
     Integer page = pageInfo.getPage();
     Integer pageSize = pageInfo.getPageSize();
}

總結(jié) 

到此這篇關(guān)于前端給后端傳數(shù)據(jù)的五種方式總結(jié)的文章就介紹到這了,更多相關(guān)前端給后端傳數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論