jQuery.ajax() 由于 WebExtension 中的 CSP 而被阻止
jQuery.ajax() blocked due to CSP within WebExtension
我正在开发 Mozilla Firefox 扩展,它需要与我在 localhost:8080 上的服务器通信。
jQuery.ajax({
type: query_method,
url: "http://localhost:8080/item",
data: item,
dataType: "jsonp",
success: function(result) {
return result.code;
},
error: function(request, status) {
/*
todo handle internal error
*/
console.log(request);
console.log(status);
}
});
感谢 CSP,我无法使用 jQuery.ajax()
到 GET/POST/DELETE/PUT
。这一切都给我以下错误信息:
Content Security Policy:
The page's settings blocked the loading of a resource at
http://localhost:8080/...
("script-src moz-extension://a79d13c4-898a-482a-9bc9-d016e8dae8f5
https://* moz-extension: blob: filesystem: 'unsafe-eval' 'unsafe-inline'").
当然,我已经尝试了一些所谓的解决方案,例如:
"content_security_policy": "script-src 'self'; object-src 'self'; report-uri http://localhost:8080"
-> 没用
"content_security_policy": "script-src 'self'; object-src 'self' http:"
-> Error processing content_security_policy: SyntaxError: ‘object-src’ directive contains a forbidden http: protocol source
任何人都可以提供发送 HTTP 请求和从 Firefox 扩展接收数据的真正解决方案吗?
为什么要使用 jQuery.ajax()` 加载资源?如果是这样,我不能使用 HTTP 协议做任何请求。
问题不在于 XHR 被 CSP 阻止,而是您正在使用 jquery 和 jsonp。如果您 allow them in the manifest, but jsonp 尝试将资源评估为 <script>
标记而不是实际执行 XHR,则 Webextensions 可以执行跨源 XHR。
摒弃 jquery,在清单中允许本地主机并使用标准化 API,例如 XHR 或 fetch()
我正在开发 Mozilla Firefox 扩展,它需要与我在 localhost:8080 上的服务器通信。
jQuery.ajax({
type: query_method,
url: "http://localhost:8080/item",
data: item,
dataType: "jsonp",
success: function(result) {
return result.code;
},
error: function(request, status) {
/*
todo handle internal error
*/
console.log(request);
console.log(status);
}
});
感谢 CSP,我无法使用 jQuery.ajax()
到 GET/POST/DELETE/PUT
。这一切都给我以下错误信息:
Content Security Policy:
The page's settings blocked the loading of a resource at
http://localhost:8080/...
("script-src moz-extension://a79d13c4-898a-482a-9bc9-d016e8dae8f5
https://* moz-extension: blob: filesystem: 'unsafe-eval' 'unsafe-inline'").
当然,我已经尝试了一些所谓的解决方案,例如:
"content_security_policy": "script-src 'self'; object-src 'self'; report-uri http://localhost:8080"
-> 没用"content_security_policy": "script-src 'self'; object-src 'self' http:"
->Error processing content_security_policy: SyntaxError: ‘object-src’ directive contains a forbidden http: protocol source
任何人都可以提供发送 HTTP 请求和从 Firefox 扩展接收数据的真正解决方案吗?
为什么要使用 jQuery.ajax()` 加载资源?如果是这样,我不能使用 HTTP 协议做任何请求。
问题不在于 XHR 被 CSP 阻止,而是您正在使用 jquery 和 jsonp。如果您 allow them in the manifest, but jsonp 尝试将资源评估为 <script>
标记而不是实际执行 XHR,则 Webextensions 可以执行跨源 XHR。
摒弃 jquery,在清单中允许本地主机并使用标准化 API,例如 XHR 或 fetch()