前端給后端傳數(shù)據(jù)的五種方式總結(jié)
更新時間:2024年07月15日 09:46:31 作者:.我非賊船
前端與后端數(shù)據(jù)交互是軟件開發(fā)中不可或缺的一環(huán),下面這篇文章主要給大家介紹了關(guān)于前端給后端傳數(shù)據(jù)的五種方式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
1.發(fā)送get請求將參數(shù)通過?拼接在url后面
$.ajax({
url: "/order/userPage?page="+page+"&pageSize="+pageSize, //請求的url地址
cache: "false", //設(shè)置為false將不會從瀏覽器中加載請求信息
async: "true", //true所有請求均為異步請求
dataType: "json", //請求返回數(shù)據(jù)的格式
type:"get", //請求方式
上面等同于==>>
async initData(){
paging: {
page: 1,
pageSize: 5
}
const res = await orderPagingApi(this.paging)
}
function orderPagingApi(data) {
return $axios({
'url': '/order/userPage',
'method': 'get',
//請求參數(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中,后臺通過占位符接收參數(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ù)序列化后傳遞到后臺。
async initData(){
const res =await formAjax();
}
function formAjax() {
$.ajax({
url: "/login",
type: "post",
data: $("#form").serialize(), // 對id為form的表單數(shù)據(jù)進行序列化并傳遞到后臺后端接收參數(shù)
@RequestMapping("/login")
@ResponseBody
//form表單的數(shù)據(jù)與User實體類的數(shù)據(jù)相對應(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ù)據(jù)相對應(yīng)
public String login ( @RequestBody User user) {}5. 前臺將普通數(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),
})后臺接收參數(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類中有屬性與其相對應(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript TypeScript實現(xiàn)貪吃蛇游戲完整詳細流程
這篇文章主要介紹了JavaScript TypeScript實現(xiàn)貪吃蛇游戲流程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習吧2022-09-09
webpack教程之webpack.config.js配置文件
本篇文章主要介紹了webpack教程之webpack.config.js配置文件 ,具有一定的參考價值,有興趣的可以了解一席2017-07-07
JS多個矩形塊選擇效果代碼(模擬CS結(jié)構(gòu))
非常不錯的可以選擇多個矩形塊的功能代碼2008-11-11
elementui-樹形控件實現(xiàn)子節(jié)點右側(cè)添加圖標和數(shù)據(jù)鼠標放上去顯示文字效果
這篇文章主要介紹了elementui-樹形控件實現(xiàn)子節(jié)點右側(cè)添加圖標和數(shù)據(jù)鼠標放上去顯示文字效果,本文結(jié)合實例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧2024-01-01

