ajax接口参数放到params、head、body里面各自写法
时间: 2023-08-05 10:07:05 浏览: 46
在使用 AJAX 发送请求时,可以通过以下方式将参数放到 params、headers、body 中:
1. params:
```javascript
$.ajax({
url: 'http://example.com/api/users',
type: 'GET',
data: {
age: 18,
gender: 'male'
}
});
```
2. headers:
```javascript
$.ajax({
url: 'http://example.com/api/users',
type: 'GET',
headers: {
'Authorization': 'Bearer xxxxxxxxxxxxxxxxxxxxxxxx'
}
});
```
3. body:
```javascript
$.ajax({
url: 'http://example.com/api/users',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
name: 'Tom',
age: 18,
gender: 'male'
})
});
```
其中,params 和 headers 在 `$.ajax()` 的第二个参数中设置,body 则需要设置 contentType 和 data 两个参数,其中 contentType 用于指定请求体的类型,data 则是请求体的内容,需要将其转换为字符串格式。