【问题标题】:Chrome Extension : Cross Origin $.Post RequestChrome 扩展程序:跨域 $.Post 请求
【发布时间】:2023-03-20 14:21:01
【问题描述】:

我正在尝试访问后来的 API,以通过 google chrome 扩展(或其他方式)将一些内容发布到站点。

因为它是一个公共 API,所以我希望它允许每个人发帖,但由于某种原因,它不允许发帖。

但是每次我得到错误。

当我从浏览器访问它时出现同样的错误

这是通过浏览器访问时的错误

http://localhost 不被 Access-Control-Allow-Origin 所允许

这是通过 chrome 扩展访问时的错误

XMLHttpRequest cannot load http://posterous.com/api/2/users/me/sites/primary/posts?api_token=vxgufysnzlDwmEtkbDbzHtxxBADsEFGr&email=***@gmail.com&password=***. Origin chrome-extension://maabelkjnhafpphacjecmcnkkmjndjgl is not allowed by Access-Control-Allow-Origin.

代码如下:

$.ajax({
        //type: 'GET',
        url: 'http://posterous.com/api/2/users/me/sites/primary/posts?api_token=vxgufysnzlDwmEtkbDbzHtxxBADsEFGr&email=shahid1376@gmail.com&password=WHY_DID_YOU_PUT_YOUR_REAL_PASS',
      // url:'http://posterous.com/api/2/users/me/sites/primary/posts/public',
       type : 'POST',

        dataType: 'json',
        crossDomain: true,
        data: "{'title': 'test posterous'}",
        contentType:'text/plain',
        success:function result(data) {
                alert("Call completed successfully"); 
        }

【问题讨论】:

  • 您实际上并没有在上面的代码中发布您的密码吗??
  • 已编辑。 ._O 你很幸运没有落入坏人之手。
  • 你是在后台运行这个吗?

标签: jquery google-chrome-extension


【解决方案1】:

您的manifest.json 文件应该包含您希望在权限中使用的两个域:

"permissions": [
    "http://*.posterous.com/",
    "http://localhost/"
]

为了它的价值,我不会在你的permissions 中包含localhost;相反,使用您计划在生产中使用的域,然后使用hosts 行将该域重定向到您自己的服务器。这将允许您在自己的服务器上进行开发,但不会强制您的用户允许他们可能不期望的localhost 上的权限。

【讨论】:

  • 没错。来自 Google 代码:code.google.com/chrome/extensions/manifest.html#permissions“如果扩展想要与页面上运行的代码进行交互,则需要。许多扩展功能,例如跨域 XMLHttpRequests、以编程方式注入的内容脚本和 cookie API 都需要主机权限。有关详细信息语法,请参阅匹配模式。”
  • Nexxeus 感谢您替换原来的通行证,它跳过了我的脑海:)
猜你喜欢
  • 2012-04-22
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
相关资源
最近更新 更多