Chrome在73版本之前一直在运行。现在它抛给我一个CORB警告,并停止运行我的chrome扩展。
下面是我的ajax jquery代码,没什么特别的
$.ajax({
url: this.url + "api/users",
type: 'get',
data: { account_id: this.account_id(), user_id: this.user_id(), person_id: person_id },
success: function (data) {
//do stuff
}
});我确实注意到,如果删除x-content-type-options头,使其不再显示为"nosniff“,则可以返回一些Ajax请求,但不返回其他请求。不确定这是否意味着什么,但我注意到返回数组的json请求可以工作,但其他请求不能。
remove_keys = %w(X-Content-Type-Options)
response.headers.delete_if{|key| remove_keys.include? key}
[{'id' : '123'}] <-worked
{'id' : '123'} <- did not work (not sure if means anything)来自chrome的完全错误
Cross-Origin Read Blocking (CORB) blocked cross-origin response https://ideas.test/api/users?token=W9BDdoiKcXLWSHXWySnwdCV69jz2y&account_id=3098355&user_id=john%40gmail.com&person_id=21046915&sync=false&new=true with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.来自响应的标头
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: x-auth_token
Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD
Access-Control-Allow-Origin: chrome-extension://mhikhjencpecbhelhjgdcgpdhlhdlhjh
Access-Control-Expose-Headers:
Access-Control-Max-Age: 1728000请求头
Provisional headers are shown
Accept: */*
Origin: chrome-extension://mhikhjencpecbhelhjgdcgpdhlhdlhjh
Referer: https://3.basecamp.com/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36如何在不使用chrome移除由于CORB而导致的正文的情况下获得要返回的响应正文?
发布于 2019-03-15 12:18:01
我找到了一种解决方法。可能对某些人来说有点过头了,但我花了15分钟才把所有东西都修好。在您的内容脚本中,将所有ajax调用包装到一个函数中:
将ajaxGet函数添加到您的内容脚本:
function ajaxGet(data){
return new Promise(function (resolve, reject) {
chrome.runtime.sendMessage({action: 'ajaxGet', data: data}, function (response) {
console.log(response)
if(response&&!response.statusText){//Might need some work here
resolve(response);
} else {
reject(response)
}
});
});
}并在您的background.js中添加一个侦听器:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if(request.action=="ajaxGet"){
$.ajax(request.data).then(sendResponse,sendResponse)
return true //telling chrome to wait till your ajax call resolves
}
})代替
$.ajax({
url: this.url + "api/user_boards",
type: 'get',
data: { account_id: this.account_id()}
}) 打电话
ajaxGet({
url: this.url + "api/user_boards",
type: 'get',
data: { account_id: this.account_id()}
}).then(onSuccess, onError) //handle response from here如果你不想在你的background.js中使用jquery,你可以调用Xhr来代替。如下所示:
var data = JSON.stringify(false);
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
sendResponse(this.responseText)
} else {
//handle errors
}
});
xhr.open("GET", request.data.url);
xhr.send(data);你必须自己解决头部的问题。
发布于 2019-03-14 10:19:51
看起来您正在将CORS标头放入请求中。相反,您需要将它们放在响应中。
发布于 2019-03-31 03:09:19
Chrome73注入了一些新的安全性。只需尝试使用chrome.runtime.sendMessage将您的xHTTP请求移动到您的后台脚本,并通过SendResponse回调获得响应。
在内容或弹出脚本中,将ajax替换为:
chrome.runtime.sendMessage(
{ action: "check", data: {/* params for url */}},
// callback with url response
function(response) {
if( response.success ) {
var myDataFromUrl = response.data;
...
} else {
console.log('Error with `check`,', response.data);
}
}
);来自后台脚本:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
var url = 'https://mysyte.com/';
if(request.action === 'check' ) {
url = url + 'check'
ajax( url, request.data,
success: function( d ) {
sendResponse({success: true, data: d});
},
error : function( d ) {
sendResponse({success: false, data: d});
}
);
}
});
function ajax( url, params, cbSuccess, cbError ) { ... }https://stackoverflow.com/questions/55153960
复制相似问题