Axios的post字符串出的小问题

本文介绍了如何使用axios库发送带有JSON格式数据的POST请求。重点在于如何将对象转换为JSON字符串并通过axios进行传递。文中提供了具体的代码示例,包括设置请求头以确保正确的内容类型。

传参的关键是JSON.Stringify,直接传递obj2是不管用的,axios接受json格式的数据

[HttpPost]
        [EnableCors("CorsSample")]
        public void Post([FromBody] string value)
        {
        }

 

let obj2="2323"

 

axios.defaults.headers.post['Content-Type'] = 'application/json';

   axios.post('http://localhost:5000/api/EmployeeInfo',JSON.stringify(obj2))

  

axios({

            headers: {

                "Content-Type": "application/json;"

            },

            method: 'post',

            url: 'http://localhost:5000/api/EmployeeInfo',

            data: JSON.stringify(obj2)

        })

 

在 Spring Boot 中,使用 `POST` 方法接收一个**字符串参数**是一个常见的需求,比如接收一段文本、JSON 字符串或纯文本数据。 --- ## ✅ 方式一:使用 `@RequestBody` 接收纯字符串 ### ✅ 后端代码 ```java @RestController public class StringController { @PostMapping("/receive-string") public String receiveString(@RequestBody String input) { return "接收到的字符串是: " + input; } } ``` ### ✅ 前端调用(使用 `axios` 或 `umi-request`) ```js axios.post('/receive-string', '这是要发送的字符串内容', { headers: { 'Content-Type': 'text/plain' } }) .then(response => { console.log(response.data); }); ``` > ⚠️ 注意:必须设置 `Content-Type: text/plain`,否则 Spring 无法识别纯字符串。 --- ## ✅ 方式二:接收 JSON 字符串并转换为 Java 字符串 如果你发送的是 JSON 字符串(比如 `"{\"name\":\"Tom\"}"`),也可以直接接收为 `String` 类型。 ### ✅ 后端代码 ```java @PostMapping("/receive-json-string") public String receiveJsonString(@RequestBody String jsonString) { return "接收到的 JSON 字符串是: " + jsonString; } ``` ### ✅ 前端调用 ```js axios.post('/receive-json-string', JSON.stringify({ name: 'Tom' }), { headers: { 'Content-Type': 'application/json' } }) .then(response => { console.log(response.data); }); ``` --- ## ✅ 方式三:使用 `@RequestParam` 接收查询参数(不推荐用于 POST) 虽然 `@RequestParam` 通常用于 `GET` 请求,但你也可以通过表单提交来接收字符串。 ### ✅ 后端代码 ```java @PostMapping("/receive-form-data") public String receiveFormData(@RequestParam String content) { return "接收到的内容: " + content; } ``` ### ✅ 前端调用(使用 `x-www-form-urlencoded`) ```js axios.post('/receive-form-data', 'content=这是内容', { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }) .then(response => { console.log(response.data); }); ``` --- ## ✅ 方式四:使用 DTO 接收(推荐用于结构化数据) 如果你希望接收结构化数据,比如一个包含字符串字段的对象,可以使用 DTO。 ### ✅ DTO 类 ```java public class TextRequest { private String content; // getter / setter } ``` ### ✅ 控制器代码 ```java @PostMapping("/receive-dto") public String receiveDto(@RequestBody TextRequest request) { return "接收到的内容: " + request.getContent(); } ``` ### ✅ 前端调用 ```js axios.post('/receive-dto', { content: '这是一段文本内容' }) .then(response => { console.log(response.data); }); ``` --- ## 🧪 总结对比表 | 接收方式 | 注解 | Content-Type | 是否推荐 | |----------|------|----------------|-----------| | 纯字符串 | `@RequestBody String` | `text/plain` | ✅ 推荐 | | JSON 字符串 | `@RequestBody String` | `application/json` | ✅ 推荐 | | 表单参数 | `@RequestParam` | `application/x-www-form-urlencoded` | ✅ 可用 | | DTO 对象 | `@RequestBody DTO` | `application/json` | ✅ 推荐用于结构化数据 | --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值