我可以从内容脚本对服务器上托管的 REST API 进行 ajax 调用吗?

From content scripts, can I make an ajax call to a REST API on hosted on my server?

在构建 chrome 扩展时阅读博客和一些 Whosebug 答案后,出于某种原因我认为我们无法对服务器上托管的 REST API 进行 ajax 调用属于托管页面以外的另一个域。这个对吗?在开发我的扩展程序时,我错误地从内容脚本调用了我的扩展程序 UI 上的按钮(UI 使用内容脚本注入到 DOM 中)。我没有运行进入任何错误。一切都很顺利。我的测试用例中的主机页面实际上是来自堆栈溢出的页面,REST API 托管在我的 localhost 上。难道是因为 api 在本地主机上?

来自 Chrome XHR documentation:

Regular web pages can use the XMLHttpRequest object to send and receive data from remote servers, but they're limited by the same origin policy. Extensions aren't so limited. An extension can talk to remote servers outside of its origin, as long as it first requests cross-origin permissions.

此外,来自Content Script documentation

Content scripts can also make cross-site XMLHttpRequests to the same sites as their parent extensions [...]

所以您唯一需要做的就是将您的 API 端点添加到清单中的主机权限:

"permissions" : [
  "*://api.example.com/*"
]