前端給后端傳數(shù)據(jù)的五種方式總結(jié)
1.發(fā)送get請(qǐng)求將參數(shù)通過?拼接在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)通過占位符接收參數(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.通過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.通過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)文章
JavaScript TypeScript實(shí)現(xiàn)貪吃蛇游戲完整詳細(xì)流程
這篇文章主要介紹了JavaScript TypeScript實(shí)現(xiàn)貪吃蛇游戲流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09
java實(shí)現(xiàn)單鏈表增刪改查的實(shí)例代碼詳解
在本篇文章里小編給大家整理了關(guān)于java實(shí)現(xiàn)單鏈表增刪改查的實(shí)例內(nèi)容,需要的朋友們可以參考下。2019-08-08
鼠標(biāo)拖拽移動(dòng)子窗體的JS實(shí)現(xiàn)
這篇文章主要介紹了鼠標(biāo)拖拽移動(dòng)子窗體的JS實(shí)現(xiàn),需要的朋友可以參考下2014-02-02
webpack教程之webpack.config.js配置文件
本篇文章主要介紹了webpack教程之webpack.config.js配置文件 ,具有一定的參考價(jià)值,有興趣的可以了解一席2017-07-07
JS多個(gè)矩形塊選擇效果代碼(模擬CS結(jié)構(gòu))
非常不錯(cuò)的可以選擇多個(gè)矩形塊的功能代碼2008-11-11
elementui-樹形控件實(shí)現(xiàn)子節(jié)點(diǎn)右側(cè)添加圖標(biāo)和數(shù)據(jù)鼠標(biāo)放上去顯示文字效果
這篇文章主要介紹了elementui-樹形控件實(shí)現(xiàn)子節(jié)點(diǎn)右側(cè)添加圖標(biāo)和數(shù)據(jù)鼠標(biāo)放上去顯示文字效果,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-01-01

