尝试在 Ionic & Angular 中对 IFTTT 执行 $http.post 时出现访问控制源问题

Access Control Origin issue when trying to do $http.post to IFTTT in Ionic & Angular

我在尝试向 IFTTT 制造商频道执行 $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 添加到 header 的各种解决方案,但仍然得到类似的响应。

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

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

Since CORS is only an issue when running your app in development mode with ionic serve, and not when running as a mobile app packaged with Cordova, a simpler option is to just disable CORS altogether for local development. For Chrome for example there's a plugin called "Allow-Control-Allow-Origin: *" that lets you disable CORS.

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

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

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

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