服务人员:在获取请求时检索xhr正文 [英] Service Workers: Retrieve xhr body when fetching the request

查看:145
本文介绍了服务人员:在获取请求时检索xhr正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检索从xhr(XMLHttpRequest)send(body)调用发送的正文? 我的xhr变量是一个XMLHttpRequest,可以使用POST方法(例如:/path/api)来调用内部url

xhr.send("a=1");

另一方面,我实现了一个Service Worker并创建了处理程序以捕获所有提取请求

self.addEventListener('fetch', function(event, body) 
{ 
event.respondWith( //Check content of event.request.body to run the right action );
}

我可以将event.request的某些属性作为event.request.url进行检索,但是我无法找到检索原始xhr正文(即"a = 1")的方法.

有趣的是,当Service Worker处理此请求并致电网络以获取结果时,

return fetch(event.request);

服务器可以访问主体数据.

在我通过SW访存方法中收到的Request对象的摘录下面

Request {method: "POST", url: "http://localhost/services", headers: Headers
, referrer: "http://localhost/m1/", referrerPolicy: "no-referrer-when-downgrade"…}

bodyUsed:false
credentials:"include"
headers:Headers
  __proto__:Headers
integrity:""
method:"POST"
mode:"cors"
redirect:"follow"
referrer:"http://localhost/path/"
referrerPolicy:"no-referrer-when-downgrade"
url:"http://localhost/path/api"

关于如何在Service Worker fetch()捕获中检索发送请求的内容/正文的任何​​建议?

谢谢!

解决方案

对于大多数人来说,这也许很明显,但是我想在响应中添加一些注释,以防将来有人遇到同样的情况.

有几种方法可以从请求中检索正文,具体取决于正文的发送方式

event.request.arrayBuffer()
event.request.blob()
event.request.json()
event.request.text()
event.request.formData()

这些方法中的任何一个都将返回Promise,其中将包括正文内容.瞧!

我还要感谢Nikhil Marathe( https://hacks.mozilla.org/2015/03/this-api-is-so-fetching/)帮助我了解所有操作的原理.

How can I retrieve the body I sent from a xhr (XMLHttpRequest) send(body) call?. My xhr variable is an XMLHttpRequest ready to call an internal url using the POST method (Ex: /path/api )

xhr.send("a=1");

On the other side, I have implemented a Service Worker and created the handler to catch all fetch requests

self.addEventListener('fetch', function(event, body) 
{ 
event.respondWith( //Check content of event.request.body to run the right action );
}

I can retrieve some properties of the event.request as event.request.url, but I am unable to find the way to retrieve my original xhr body (i.e. "a=1").

Interestingly, when the Service Worker handles this request, and calls the network to get the result,

return fetch(event.request);

the server get access to the body data.

Below an extract of the Request object I receive within the SW fetch method

Request {method: "POST", url: "http://localhost/services", headers: Headers
, referrer: "http://localhost/m1/", referrerPolicy: "no-referrer-when-downgrade"…}

bodyUsed:false
credentials:"include"
headers:Headers
  __proto__:Headers
integrity:""
method:"POST"
mode:"cors"
redirect:"follow"
referrer:"http://localhost/path/"
referrerPolicy:"no-referrer-when-downgrade"
url:"http://localhost/path/api"

Any suggestion on how to retrieve the content/body of the send request within the Service Worker fetch() capture?

Thanks!

解决方案

This is maybe obvious for most people, but I wanted to add some notes with the response, in case someone is in my same situation in the future.

There are several methods to retrieve the body from the request, depending on how the body has been sent

event.request.arrayBuffer()
event.request.blob()
event.request.json()
event.request.text()
event.request.formData()

Any of those methods will return a Promise, which will include the body content. Voila!

I need to thank also Nikhil Marathe (https://hacks.mozilla.org/2015/03/this-api-is-so-fetching/) for helping me understand how all this works.

这篇关于服务人员:在获取请求时检索xhr正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆