前端请求传参格式

格式类型

form-data
application/x-www-form-urlencoded
application/json
text/xml

一、form-data

  1. multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分,这个一般文件上传时用。

  2. 当method为post时候,浏览器把form数据封装到http body中,然后发送到server。

  3. 如果没有type=file的控件,默认用application/x-www-form-urlencoded。

  4. 如果有type=file,必须用multipart/form-data。

调用接口的时候参数data等于formData,用console.log打印formdata为空,其实是有数据的

const formData = new FormData();
formData.append('name', '李雷');

// 使用get方法查看数据
formData.get('modelName') // code

二、application/x-www-form-urlencoded

①请求方式为get:
当method为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2…),然后把这个字串append到url后面,用?分割,加载这个新的url,可以传json对象格式

②post
为post的情况需要传form-data格式, 窗体数据被编码为名称/值对,这是标准的编码格式
1.可以new FormData()

2.可以使用 qs.stringify() 转成键值的格式
qs需安装 命令:npm run qs

let obj ={name:'Cathy',age:100}
let qsResult = qs.stringify(obj)  // name=Cathy&age=100
let jsonResult = JSON.stringify(obj) // '{"name":"Cathy","age":100}'
aaa(qsResult).then(res={}}

需要手动修改content-type

export function listTreeByDimensionId(query) {
    return request({
      url: '/setup/kgceDimensionTree/listTreeByDimensionId',
      method: 'post',
      data: query,
      headers:{
        'Content-Type':'application/x-www-form-urlencoded' 
      }
    })
}

三、application/json

普通json格式传参

四、text/xml

是一种使用 HTTP 作为传输协议,XML 作为编码方式的远程调用规范。

是一种使用 HTTP 作为传输协议,XML 作为编码方式的远程调用规范

四、Content-Type的使用
客户端发送请求(Request)时的Content-Type设置,特别是使用ajax的时候,如果设置得不准确,很有可能导致请求失败。

  1. 如果是一个restful接口(json格式),一般将Content-Type设置为application/json; charset=UTF-8;
  2. 如果是文件上传,一般Content-Type设置为multipart/form-data
  3. 如果普通表单提交,一般Content-Type设置为application/x-www-form-urlencoded
    参考:https://blog.csdn.net/qq_45796667/article/details/125239247

遇到问题:

1.get请求修改Content-Type无效
请求方法中设置了请求头Content-Type,request headers中没有更改成功

export function detailBy() {
    return request({
      url: '/setup/kgceDimension/detail',
      method: 'get',
      headers: {
    	'Content-Type': 'application/json;charset=UTF-8'
      },
    })
}

2.问题原因:
npm下axios的源码中,当未设置请求数据的时候会删掉Content-Type的设置

3.解决方法
config.data = true

/**
 * request interceptor 发送请求时拦截
 */
service.interceptors.request.use(
  config => {
    if (!config.data) {
      config.data = true // 解决请求没有参数时添加不上Content-Type问题
    }
    // config.headers['Content-type'] = 'application/json;charset=UTF-8'
    config.headers['Authorization-Idaas'] = getToken()
    return config
  },
  error => {
    // do something with request error
    return Promise.reject(error)
  }
)

2.axios封装问题
不要设备默认Content-type,因为遇到不同的请求类型时,会被覆盖,修改无效。axios默认Content-type为application/json

/**
 * request interceptor 发送请求时拦截
 */
service.interceptors.request.use(
  config => {
    // config.headers['Content-type'] = 'application/json;charset=UTF-8'//不要设备默认Content-type
    config.headers['Authorization-Idaas'] = getToken()
    return config
  },
  error => {
    // do something with request error
    return Promise.reject(error)
  }
)

3. axios默认headers content-type问题
①传参为对象,请求头的content-type是application/json;charset=UTF-8
②发送一个上传文件的请求formdata,请求头的content-type是multipart/form-data
参考:https://blog.csdn.net/u010856177/article/details/127074333

### 回答1: RequestParam是Spring MVC框架中用来获取请求参数的注解,前端可以通过发送HTTP请求,在请求的URL中携带参数,例如: GET /api/user?id=123&name=张三 HTTP/1.1 其中,id和name就是请求参数,可以通过@RequestParam注解在后端控制器中获取到。前端可以通过拼接URL的方式将参数传递给后端,也可以通过POST请求,在请求体中携带参数。例如: POST /api/user HTTP/1.1 Content-Type: application/json { "id": 123, "name": "张三" } 后端控制器可以通过@RequestParam注解获取请求参数,例如: @GetMapping("/api/user") public User getUser(@RequestParam("id") Long id, @RequestParam("name") String name) { // 根据id和name查询用户信息 } 其中,@RequestParam注解中的value属性指定了请求参数的名称,如果不指定,默认使用方法参数的名称作为请求参数的名称。 ### 回答2: RequestParam是Spring MVC中接收HTTP请求参数的注解之一,它可以用于从请求中获取参数并将其绑定到Controller方法的参数上。RequestParam注解需要在Controller方法的参数前添加,如下所示: ``` @RequestMapping(value = "/users", method = RequestMethod.GET) public String getUsers(@RequestParam("username") String username, @RequestParam("age") int age) { // 使用username和age进行业务操作 return "user_list"; } ``` @RequestParam注解中的value属性用于指定请求参数的名称。对于前端传参,有以下几种方式: 1. 通过URL路径传参 可以在请求的URL路径中携带参数,如下所示: ``` @GetMapping("/users/{username}") public String getUserByName(@PathVariable("username") String username) { // 使用username进行业务操作 return "user_detail"; } ``` 2. 通过查询参数传参 可以在请求的URL路径的?后携带参数,参数以key=value的形式出现,多个参数用&连接,如下所示: ``` @GetMapping("/users") public String getUserByUsernameAndAge(@RequestParam("username") String username, @RequestParam("age") int age) { // 使用username和age进行业务操作 return "user_list"; } ``` 则前端通过GET方法,请求的URL为"/users?username=test&age=18" 3. 通过表单提交传参 可以通过表单提交方式传参,如下所示: ``` @PostMapping("/users") public String addUser(@RequestParam("username") String username, @RequestParam("age") int age) { // 使用username和age进行业务操作 return "user_detail"; } ``` 前端可以通过form表单的input标签传参。 以上三种方式都可以使用@RequestParam注解来接收参数,但实现的方式不同。用户选择哪种方式取决于具体的业务场景和需求。 ### 回答3: @RequestParam是Spring框架中用于接收前端传来请求参数的注解。使用@RequestParam注解可以从请求参数中获取参数的值。在接收前端传来的参数时,需要用@RequestParam注解来声明参数,然后将参数名赋给@RequestParam中的value值即可。下面是一个示例代码: @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET) public String getUserInfo(@RequestParam(value = "userId", required = true) String userId, Model model) { //根据userId查询对应的用户信息,将查询结果添加到model中 UserInfo userInfo = userService.getUserInfoById(userId); model.addAttribute("userInfo", userInfo); return "userInfo"; } 在上面的示例中,@RequestParam注解中的value值为"userId",将前端传来的参数名"userId"与注解中的value值对应,表示从请求参数中获取名为"userId"的参数值。required属性表示是否必传,必传时如果前端不传递则会抛出异常。如果不设置该属性默认为true,即必传。 前端传递请求参数时,需要将参数名和参数值以键值对的形式放在请求的URL中。例如,上述例子中的请求URL可能是像这样的: http://localhost:8080/getUserInfo?userId=12345 其中"userId"就是我们在@RequestParam注解中设置的参数名,而"12345"则是前端传递的参数值。 在使用@RequestParam注解的时候,需要注意一下几点: 1. 请求参数需要和@RequestParam注解中的value值对应 2. 注解中还有其他属性可以设置,如defaultValue属性可以设置参数的默认值 3. 可以使用@RequestParam注解来接收基本数据类型、String、数组和集合等类型的请求参数。 总之,@RequestParam注解是Spring框架中接收前端请求参数的重要方式,掌握其使用方法能够有效地提高后端接口开发的效率。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值