最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

前端給后端傳數(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í)體類(lèi)的數(shù)據(jù)相對(duì)應(yīng)
public String login (User user) {}

4.通過(guò)post提交方式將form表單的類(lèi)型是 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í)體類(lèi)的數(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類(lèi)中有屬性與其相對(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)論

安丘市| 平利县| 仪陇县| 岑巩县| 平罗县| 比如县| 巴南区| 寿阳县| 雷州市| 怀柔区| 望奎县| 乾安县| 德庆县| 京山县| 灵宝市| 铅山县| 宁都县| 杭州市| 朝阳县| 桐梓县| 托里县| 图木舒克市| 黄骅市| 沅江市| 沂水县| 临夏市| 牡丹江市| 措美县| 广平县| 新竹县| 胶南市| 瑞安市| 班戈县| 邢台市| 鱼台县| 息烽县| 偏关县| 岳普湖县| 黄山市| 沂南县| 章丘市|