由于安全问题,sendBeacon API暂时无法使用,是否有任何解决方法? [英] sendBeacon API not working temporarily due to security issue, any workaround?

查看:460
本文介绍了由于安全问题,sendBeacon API暂时无法使用,是否有任何解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,使用sendBeacon方法发送异步HTTP请求,

I have the following code to send asynchronous HTTP request using sendBeacon method,

var data = {
 name: 'test',
 uniqueId: Math.random()
};
var blob = new Blob([JSON.stringify(data)], {type : 'application/json'});
navigator.sendBeacon('http://example.in/data/post', blob);

这段代码已经运行了很长时间了.当前,由于chrome中的安全问题 https://bugs.chromium.org /p/chromium/issues/detail?id = 490015 ,我们看到错误"无法在'Navigator'上执行'sendBeacon':sendBeacon()的类型不是CORS安全列出的MIME类型的Blob通过实验禁止.有关详情,请参见 http://crbug.com/490015 ."

This code has worked fine for a long time. Currently, due to security issue in chrome https://bugs.chromium.org/p/chromium/issues/detail?id=490015, we see error "Failed to execute 'sendBeacon' on 'Navigator': sendBeacon() with a Blob whose type is not CORS-safelisted MIME type is disallowed experimentally. See http://crbug.com/490015 for details."

在解决此问题之前,是否有任何变通办法通过使用相同的sendBeacon API修改请求标头来发送JSON数据?对于依赖此API的网站,在进行修复之前,继续使用将很有用.使用XHR发布数据的建议没有用.

Is there any workaround to send JSON data by modifying request headers using same sendBeacon API till the issue is fixed? It'll be useful for sites depending on this API to continue to use till a fix is made. Suggestions on using XHR to post data are not useful.

推荐答案

现在sendBeacon中Content-Type标头的唯一允许值为:

The only allowed values for the Content-Type header in sendBeacon now are:

  • application/x-www-form-urlencoded
  • multipart/form-data
  • 文本/纯文本

我在项目中遇到了类似的问题,最终以文本/纯文本"形式发送了数据; charset = UTF-8',并在服务器端读取流以获取json内容.

I had a similar issue in our project and I ended up sending the data as 'text/plain; charset=UTF-8' and reading the stream on server side for the json content.

客户:

const blob = new Blob([JSON.stringify(myData)], { type: 'text/plain; charset=UTF-8' });
navigator.sendBeacon(appData.ReleaseSessionUrl, blob);

服务器:

using (var reader = new StreamReader(this.Request.InputStream))
{
   var jsonData = reader.ReadToEnd();
   var sessionData = JsonConvert.DeserializeObject<MyDataType>(jsonData);
}

不确定这是否对您有帮助.

Not sure if this helps you.

https://github.com/GoogleCloudPlatform/stackdriver-errors-js/问题/10

这篇关于由于安全问题,sendBeacon API暂时无法使用,是否有任何解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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