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

解決axios發(fā)送post請(qǐng)求,springMVC接收不到數(shù)據(jù)問題的處理

 更新時(shí)間:2026年05月20日 10:38:08   作者:svygh123  
文章主要討論了Vue組件無法正確接收和處理Axios請(qǐng)求的問題,并詳細(xì)描述了SpringMVC中使用@PathVariable、@RequestBody、@RequestParam的不同場(chǎng)景及其對(duì)應(yīng)的前端Axios寫法

今天發(fā)現(xiàn)一個(gè)問題:

vue組件中無法正確接收并處理axios請(qǐng)求

這個(gè)問題已經(jīng)困擾我好久了,在電腦面前坐了兩天只能確定前端應(yīng)該是正確的發(fā)送了請(qǐng)求,但發(fā)送請(qǐng)求后無法正確接受后端返回的數(shù)據(jù)。

問題:vue組件無法接受后端數(shù)據(jù)

錯(cuò)誤代碼如下:

axios.post("/simple_query",{
},this.simple_query_input).then(res=>{    
    console.log(res);
}).catch(err=>{
    console.log(err);
})
@RequestMapping(value = "/simple_query",method = RequestMethod.POST)
public cheName handleSimpleQuery(@RequestParam("simple_query_input") String simpleQueryInput) throws Exception {

}

網(wǎng)上找到也有類似未解決的:

Spring MVC的Controller里面使用了@RequestParam注解來接收參數(shù),但是只在GET請(qǐng)求的時(shí)候才能正常訪問,在使用POST請(qǐng)求的時(shí)候會(huì)產(chǎn)生找不到參數(shù)的異常。原本好好的POST請(qǐng)求開始報(bào)400錯(cuò)誤,找不到REST服務(wù),一般情況下報(bào)這種錯(cuò)誤多是由于POST請(qǐng)求時(shí)傳遞的參數(shù)不一致,但是這次不存在這種問題,百思不得其解啊。。。

還有這個(gè):axios發(fā)送post請(qǐng)求,springMVC接收不到數(shù)據(jù)問題

@RequestMapping(method = RequestMethod.POST, value = "/test")
@ResponseBody
public ResponseEntity testDelete(@RequestParam("id") Integer id)
        throws Exception {
    return ResponseEntity.ok();
}

代碼中是規(guī)定了請(qǐng)求方式POST,使用@RequestParam接收id參數(shù)。

然后前臺(tái)請(qǐng)求參數(shù)也對(duì),是這個(gè)形式的{id:111},看起來沒錯(cuò)啊,參數(shù)名完全一樣,但是后臺(tái)報(bào)錯(cuò)Required String parameter 'id' is not present,說id參數(shù)必須傳過來。

分析問題

雖然前端傳遞的參數(shù)形式為{id: 111},看起來id的參數(shù)名確實(shí)是一樣的,但是這個(gè)參數(shù)是作為請(qǐng)求的body而非作為普通參數(shù)或query parameter傳遞的。因此無法直接使用@RequestParam注釋接收它。

今天就倆解決一下吧:

SpringMVC@PathVariable、@RequestBody、@RequestParam的使用場(chǎng)景以及對(duì)應(yīng)的前端axios寫法是什么呢?

一、@PathVariable

axios代碼:

axios.post('http://localhost:8080/endpoint3/' + this.firstName + '/' + this.lastName)
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error(error);
});	

SpringMvc的controller代碼:

@PostMapping("/endpoint3/{firstName}/{lastName}")
@ResponseBody
public String endpoint2(@PathVariable("firstName") String firstName,  
         @PathVariable("lastName") String lastName) {
	// 處理請(qǐng)求邏輯
	return "Hello, " + firstName + " " + lastName;
}

二、@RequestBody

axios代碼:

axios.post('http://localhost:8080/endpoint2', {firstName: this.firstName, lastName: this.lastName})
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error(error);
});	

SpringMvc的controller代碼:

@PostMapping("/endpoint2")
@ResponseBody
public String endpoint2(@RequestBody Map<String, Object> requestBody) {
	String firstName = Objects.toString(requestBody.get("firstName"));
	String lastName = Objects.toString(requestBody.get("lastName"));
	// 處理請(qǐng)求邏輯
	return "Hello, " + firstName + " " + lastName;
}

三、@RequestParam

axios代碼:

const formData = new FormData();
formData.append('firstName', this.firstName);
formData.append('lastName', this.lastName);
axios.post('http://localhost:8080/endpoint1', formData)
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error(error);
});	

SpringMvc的controller代碼:

@PostMapping("/endpoint1")
public String handlePostRequest(@RequestParam("firstName") String firstName,
		@RequestParam("lastName") String lastName) {
	// 處理請(qǐng)求邏輯
	return "Hello, " + firstName + " " + lastName;
}

前臺(tái) 完整代碼:

<!DOCTYPE html>
<html>
<head>
	<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.7.0/vue.min.js"></script>
	<script src="https://cdn.bootcdn.net/ajax/libs/axios/1.6.8/axios.min.js"></script>
</head>
<body>
	<div id="app">
		
	</div>
	<script>
			new Vue({
			  el: '#app',
			  data () {
				return {
				  firstName: "John",
				  lastName: "Doe"
				}
			  },
			  mounted () {
				  const formData = new FormData();
				  formData.append('firstName', this.firstName);
				  formData.append('lastName', this.lastName);
				  axios.post('http://localhost:8080/endpoint1', formData)
					.then(response => {
					  console.log(response.data);
					})
					.catch(error => {
					  console.error(error);
					});	

					axios.post('http://localhost:8080/endpoint2', {firstName: this.firstName, lastName: this.lastName})
					.then(response => {
					  console.log(response.data);
					})
					.catch(error => {
					  console.error(error);
					});	

					axios.post('http://localhost:8080/endpoint3/' + this.firstName + '/' + this.lastName)
					.then(response => {
					  console.log(response.data);
					})
					.catch(error => {
					  console.error(error);
					});	

			  }
			})
	</script>

</body>

后臺(tái)核心代碼:

@RestController
@CrossOrigin
public class MySpringMvcController {
	
	@PostMapping("/endpoint1")
	public String handlePostRequest(@RequestParam("firstName") String firstName,
			@RequestParam("lastName") String lastName) {
		// 處理請(qǐng)求邏輯
		return "Hello, " + firstName + " " + lastName;
	}
	
	@PostMapping("/endpoint2")
	@ResponseBody
	public String endpoint2(@RequestBody Map<String, Object> requestBody) {
		String firstName = Objects.toString(requestBody.get("firstName"));
		String lastName = Objects.toString(requestBody.get("lastName"));
		// 處理請(qǐng)求邏輯
		return "Hello, " + firstName + " " + lastName;
	}
	
	@PostMapping("/endpoint3/{firstName}/{lastName}")
	@ResponseBody
	public String endpoint2(@PathVariable("firstName") String firstName,  
	         @PathVariable("lastName") String lastName) {
		// 處理請(qǐng)求邏輯
		return "Hello, " + firstName + " " + lastName;
	}

}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring中配置和讀取多個(gè)Properties文件的方式方法

    Spring中配置和讀取多個(gè)Properties文件的方式方法

    本篇文章主要介紹了Spring中配置和讀取多個(gè)Properties文件的方式方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • java &與&&的區(qū)別及實(shí)例

    java &與&&的區(qū)別及實(shí)例

    這篇文章主要介紹了java &與&&的區(qū)別的相關(guān)資料,并附簡單實(shí)例,幫助大家學(xué)習(xí)理解這部分知識(shí),需要的朋友可以參考下
    2016-10-10
  • Flowable流程引擎API與服務(wù)

    Flowable流程引擎API與服務(wù)

    這篇文章主要介紹了Flowable流程引擎API與服務(wù),引擎API是與Flowable交互的最常用手段,總?cè)肟邳c(diǎn)是ProcessEngine,使用ProcessEngine,可以獲得各種提供工作流或BPM方法的服務(wù),下面我們來詳細(xì)了解
    2023-10-10
  • springboot2中HikariCP連接池的相關(guān)配置問題

    springboot2中HikariCP連接池的相關(guān)配置問題

    這篇文章主要介紹了springboot2中HikariCP連接池的相關(guān)配置問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java并發(fā)工具類Phaser詳解

    Java并發(fā)工具類Phaser詳解

    這篇文章主要介紹了Java并發(fā)工具類Phaser詳解,Phaser(階段協(xié)同器)是一個(gè)Java實(shí)現(xiàn)的并發(fā)工具類,用于協(xié)調(diào)多個(gè)線程的執(zhí)行,它提供了一些方便的方法來管理多個(gè)階段的執(zhí)行,可以讓程序員靈活地控制線程的執(zhí)行順序和階段性的執(zhí)行,需要的朋友可以參考下
    2023-11-11
  • Java切面(Aspect)的多種實(shí)現(xiàn)方式

    Java切面(Aspect)的多種實(shí)現(xiàn)方式

    這篇文章主要給大家介紹了關(guān)于Java切面(Aspect)的多種實(shí)現(xiàn)方式,在Java開發(fā)中切面(Aspect)是一種常用的編程方式,用于實(shí)現(xiàn)橫切關(guān)注點(diǎn)(cross-cutting concern),需要的朋友可以參考下
    2023-08-08
  • SpringBoot的自動(dòng)裝配機(jī)制和Starter的實(shí)現(xiàn)原理分析

    SpringBoot的自動(dòng)裝配機(jī)制和Starter的實(shí)現(xiàn)原理分析

    SpringBoot自動(dòng)裝配機(jī)制通過`@EnableAutoConfiguration`開啟,基于條件化配置實(shí)現(xiàn),簡化開發(fā),Starter作為“開箱即用”的依賴模塊,包含庫依賴、自動(dòng)配置類和附加配置,自動(dòng)裝配與Starter結(jié)合,用戶只需引入Starter即可獲得完整功能,提升開發(fā)效率
    2026-05-05
  • Java?9中List.of()的使用示例及注意事項(xiàng)

    Java?9中List.of()的使用示例及注意事項(xiàng)

    Java 9引入了一個(gè)新的靜態(tài)工廠方法List.of(),用于創(chuàng)建不可變的列表對(duì)象,這篇文章主要介紹了Java?9中List.of()的使用示例及注意事項(xiàng)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-03-03
  • MyBatis插入Insert、InsertSelective的區(qū)別及使用心得

    MyBatis插入Insert、InsertSelective的區(qū)別及使用心得

    這篇文章主要介紹了MyBatis插入Insert、InsertSelective的區(qū)別及使用心得,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • MyBatis配置文件元素示例詳解

    MyBatis配置文件元素示例詳解

    在MyBatis框架的核心配置文件中,<configuration>元素是配置文件的根元素,其他元素都要在<contiguration>元素內(nèi)配置,這篇文章主要介紹了MyBatis配置文件元素,需要的朋友可以參考下
    2023-06-06

最新評(píng)論

瓦房店市| 阿拉尔市| 弥勒县| 凉山| 临西县| 报价| 商城县| 松溪县| 阳山县| 德兴市| 杨浦区| 平山县| 大理市| 保靖县| 湾仔区| 巴青县| 伊通| 卫辉市| 通道| 汝州市| 三门峡市| 东丰县| 上栗县| 延庆县| 大石桥市| 封开县| 墨竹工卡县| 望江县| 宝清县| 平凉市| 保山市| 藁城市| 和硕县| 乃东县| 永兴县| 民丰县| 德惠市| 独山县| 信阳市| 蓝山县| 舟曲县|