java setrequestheader_Ajax中的setRequestHeader设置请求头

1、问题引发点:

前不久发现一个问题: 前端并没有设置请求头信息里面的Accept-Encoding:gzip...但是在请求头中可以明显的看到Accept-Encoding:gzip, deflate, sdch,并且我尝试修改这个请求头,发现 不 生 效;

2、XMLHttpRequest对象提供了一个设置请求头的方法:setRequestHeader,对应的jQuery可以再beforeSend回调里面设置请求头:

1

2

3

4

5

6

7

8

9

10

$.ajax({

type: "GET",

url: "test.php",

success: function(data) {

console.log(data);

},

beforeSend: function(xhr) {

xhr.setRequestHeader("User-Agent", "headertest");

}

});

3、后来看W3C标准文档发现,这个请求头不是什么都可以设置的,标准里面明确规定了以下请求头信息是浏览器控制,开发者不允许设置这些请求头

Terminate these steps if header is a case-insensitive match for one of the following headers:

Accept-Charset

Accept-Encoding

Access-Control-Request-Headers

Access-Control-Request-Method

Connection

Content-Length

Cookie

Cookie2

Date

DNT

Expect

Host

Keep-Alive

Origin

Referer

TE

Trailer

Transfer-Encoding

Upgrade

User-Agent

Via

… or if the start of header is a case-insensitive match for Proxy- or Sec- (including when header is just Proxy- or Sec-).

The above headers are controlled by the user agent to let it control those aspects of transport. This guarantees data integrity to some extent. Header names starting with Sec- are not allowed to be set to allow new headers to be minted that are guaranteed not to come fromXMLHttpRequest.

4、例子:

testAE.html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

html>

test

$.ajax({

type: "GET",

url: "./testAE.php",

success: function(data) {

$("body").append(data);

},

beforeSend: function(xhr) {

xhr.setRequestHeader("Accept-Encoding", "testAE");

}

});

testAE.php

1

2

3

4

/*回传ACCEPT_ENCODING*/

echo $_SERVER['HTTP_ACCEPT_ENCODING'];

?>

方法二:

headers: {

'token':'token_value'

},

AJAX(Asynchronous JavaScript and XML)是一种前端技术,用于创建无刷新、异步的数据交换,使得网页可以在后台与服务器通信而无需刷新整个页面。在JavaAjax通常通过JavaScript库如jQuery、axios等发送HTTP请求。 对于设置接受流数据(比如文件上传或大块文本),你需要在发送请求时指定正确的Content-Type,并在后端处理部分配置相应的解析。例如,在发送POST请求时,你可以这样做: ```javascript var xhr = new XMLHttpRequest(); xhr.open('POST', 'your-url', true); xhr.setRequestHeader('Content-Type', 'multipart/form-data'); // 或者 'application/octet-stream' for binary data // 定义处理响应的函数 xhr.onload = function () { if (xhr.status === 200) { var responseStream = xhr.response; // 这是一个Blob对象,需要进一步处理 handleDataStream(responseStream); } else { console.error('Request failed. Status:', xhr.status); } }; xhr.send(fileOrBinaryData); // fileOrBinaryData是你想发送的流数据 ``` 后端(如Spring MVC、RESTful API)需要解析这个流数据,这取决于使用的框架。例如,在Spring MVC,可以使用`MultipartFile`处理文件上传: ```java @PostMapping("/upload") public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) { try { // 对文件进行处理... return ResponseEntity.ok("File uploaded successfully"); } catch (Exception e) { return ResponseEntity.badRequest().body(e.getMessage()); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值