0

我在尝试向 IFTTT maker 频道执行 $http.post 时遇到问题。下面是我用来执行 POST 的代码:

$http.post(
    'https://maker.ifttt.com/trigger/{my-event}/with/key/{my-key}',
    {value1:"hello",value2:"goodbye"}
).then(
     function successCallback(response) {
        console.log(response);
     }, 
     function errorCallback(response) {
        console.log("error: ",response);
     });

我得到的响应如下:

XMLHttpRequest cannot load https://maker.ifttt.com/trigger/{my-event}/with/key/{my-key}. 
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

我有 cordova-whitelist 插件,并尝试了添加Access-Control-Origin到标题的各种解决方案,但仍然得到类似的响应。

4

1 回答 1

3

CORS 问题仅存在于浏览器上。在这种情况下,它们只存在于使用ionic serve.

将应用程序部署到实际设备时,不会出现 CORS 错误。

由于 CORS 仅在使用 ionic serve 在开发模式下运行应用程序时才会出现问题,而不是在作为与 Cordova 打包的移动应用程序运行时出现问题,因此更简单的选择是完全禁用 CORS 以进行本地开发。例如,对于 Chrome,有一个名为“Allow-Control-Allow-Origin: *”的插件可以让你禁用 CORS。

如果您仍想以代码方式处理 cors,您可以在此处获取更多信息http://blog.ionic.io/handling-cors-issues-in-ionic/

考虑到这一点,这是为 mozilla 或 chrome 获取 CORS 插件的更快更好的方法。你可以在这里得到它们

铬 - https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

Mozilla - https://addons.mozilla.org/en-US/firefox/addon/cors-everywhere/

于 2016-10-20T14:53:58.510 回答