【问题标题】:Ionic http CORS same origin not allowed离子 http CORS 同源不允许
【发布时间】:2016-12-07 21:44:52
【问题描述】:

我正在尝试在 ionic 应用程序中设置 Mailgun mail service。这是代码: 控制器:

$http({
                        "method": "POST",
                        "url": "https://api.mailgun.net/v3/" + mailgunUrl + "/messages",
                        //"crossDomain": "true",
                        "headers": {
                            "Access-Control-Allow-Origin": "*",//"http://localhost:8100",
                            "Access-Control-Allow-Headers": "content-type, accept",
                            //"Access-Control-Allow-Credentials": "true",
                            "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE",
                            "Content-Type": "application/x-www-form-urlencoded",
                            'Authorization' : 'Basic '+ mailgunApiKey
                            //"Authorization": "Basic " + mailgunApiKey//$base64.encode('api:key-'+mailgunApiKey)
                        },
                        data: "from=" + "no-reply@ineevent.com" + "&to=" + $scope.datapopup.mail + "&subject=" + "Guestlist" + "&text=" 

config.xml

<content src="main.html"/>
  <access origin="*"/>
  <plugin name="cordova-plugin-whitelist" version="1"/>
  <plugin name="cordova-plugin-crop" spec="~0.1.0"/>
  <allow-navigation href="*" />
  <allow-intent href="*"/>
  <allow-intent href="http://*/*"/>
  <allow-intent href="https://*/*"/>
  <allow-intent href="tel:*"/>
  <allow-intent href="sms:*"/>
  <allow-intent href="mailto:*"/>
  <allow-intent href="geo:*"/>

我收到 status '0' error 并且日志显示 CORS(跨域请求)在缺少 access-control-allow-headers 的情况下不允许(从法语翻译)。

Chrome 的另一个错误是'Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response.'

我也尝试从 android 设备但无法正常工作。有什么想法吗?

【问题讨论】:

    标签: ionic-framework cors mailgun


    【解决方案1】:

    将此添加到 index.html

      <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
    

    并将cordova-plugin-whitelist 插件添加到您的应用中。


    出于开发目的,您可以将 CORS plugin 用于 Chrome。


    你的控制器可以是这样的:

    .controller("EmailCtrl", function($scope, $http) {
    
        var mailgunUrl = "YOUR_DOMAIN_HERE";
        var mailgunApiKey = window.btoa("api:key-YOUR_API_KEY_HERE")
    
        $scope.recipient = "mail@gmail.com";
    
        $scope.send = function(recipient, subject, message) {
            $http(
                {
                    "method": "POST",
                    "url": "https://api.mailgun.net/v3/" + mailgunUrl + "/messages",
                    "headers": {
                        "Content-Type": "application/x-www-form-urlencoded",
                        "Authorization": "Basic " + mailgunApiKey
                    },
                    data: "from=" + "ionic@example.com" + "&to=" + recipient + "&subject=" + subject + "&text=" + message
                }
            ).then(function(success) {
                console.log("SUCCESS " + JSON.stringify(success));
            }, function(error) {
                console.log("ERROR " + JSON.stringify(error));
            });
        }
    
    })
    

    【讨论】:

    • 我看到您找到了解决方案 :) 。几个月前我写了一篇关于 Mailgun 和 Ionic 的博客文章,所以我知道它必须有效。 @夜空代码?
    • 为什么对我的 4 个答案投反对票 postimg.cc/image/h6yuq3ir5?对于上面的答案仍然可以理解,但对于我的其他答案则不是。 NightSkyCode 的第一条评论是关于某人是个混蛋。这就是为什么我在他的昵称上打上问号,因为不清楚他指的是谁。我可以看到 NightSkyCode stackoverflow.com/users/1530143/nightskycode 的帐户已暂停“此帐户已暂停以降温。暂停期将于 9 月 1 日 15:41 结束。”
    【解决方案2】:

    答案很简单:

    当你从 ionic 运行 android 时,你不应该使用任何可选参数: 离子运行android

    【讨论】:

      猜你喜欢
      • 2018-07-28
      • 2018-08-11
      • 2019-10-27
      • 2012-11-04
      • 2016-08-05
      • 2020-08-23
      • 2021-04-28
      • 2021-09-07
      • 2013-01-29
      相关资源
      最近更新 更多