0

我正在开发一个Mozilla Firefox扩展,它需要与localhost上的服务器进行通信:8080。jQuery.ajax()由于WebExtension中的CSP而被阻止

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协议来做任何请求。

回答

3

问题不在于CSP阻止XHR,而在于您使用的是jquery和jsonp。如果您allow them in the manifest,则Webextensions可以执行跨源XHR,但jsonp会尝试将资源评估为<script>标记,而不是实际执行XHR。

沟渠jquery,允许本地主机在清单和使用标准化的API,如XHR或fetch()

相关问题