【问题标题】:Couldn't get rid of the multi-origins error when sending an ajax request to mailjet向 mailjet 发送 ajax 请求时无法摆脱多源错误
【发布时间】:2015-04-29 17:25:50
【问题描述】:

我正在使用 AJAX 请求在我的 Web 应用程序中尝试使用 mailjet(因为 mandrill 测试不成功,因为发送的电子邮件仍然“排队”并且永远不会到达邮箱)邮件服务:

             $.ajax({

                  // The 'type' property sets the HTTP method.
                  // A value of 'PUT' or 'DELETE' will trigger a preflight request.
                  type: 'POST',

                  // The URL to make the request to.
                  url: 'https://api.mailjet.com/v3/send/message',

                  contentType: 'text/plain',

                  data: {
                    'user': '10ca83...public key 1ccd945:f58e84f9d...private key..8e9502fd7a',
                    'from': emaildata.email,
                    'to': 'recipiant@gmail.com',
                    'subject': 'mailjet test',
                    'text': emaildata.msg                   
                  },

                  xhrFields: {                  
                    withCredentials: false
                  },

                  headers: {

                  },

                  success: function() {
                    // Here's where you handle a successful response.
                  },

                  error: function() {
                    // Here's where you handle an error response.
                    // Note that if the error was due to a CORS issue,
                    // this function will still fire, but there won't be any additional
                    // information about the error.
                  }
                }).done(function(response) {
                   console.log(response); // if you're into that sorta thing
                 });
        }

当我在浏览器控制台中遇到此错误时:

index.php:1 XMLHttpRequest cannot load https://api.mailjet.com/v3/send/message. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 401.

我试图找到一个解决方案来摆脱它,我找到了两个解决方案: 1/ 使用 CORS。 2/ 我还发现从本地网页发送请求可能会出现问题,所以我运行了一个 wamp 服务器并直接在浏览器中启动了应用程序。

但我仍然有这个错误:/有什么建议吗? 接下来我将尝试使用 google gmail API 发送电子邮件。

【问题讨论】:

  • 查看dev.mailjet.com 中提供的文档 - 我认为 API 不能在客户端 javascript 中使用
  • 意思是我认为,你需要使用服务器端技术来与 API 对话,这不是不合理的大多数其他 API 都会有相同的要求

标签: javascript ajax mailjet


【解决方案1】:

查看http://dev.mailjet.com 中提供的文档 - 我认为该 API 不能用于客户端 javascript。

我认为,您需要使用服务器端技术与 API 通信,这并非不合理,大多数其他 API 都会有相同的要求。

如果您要设置 mailjet,他们已经为它编写了一个 php 包装器https://github.com/mailjet/mailjet-apiv3-php-simple

【讨论】:

  • 他们的技术代理这么说,但安全问题对我的应用来说并不重要。
  • 我猜你的代码不起作用的原因是不支持javascript客户端Ajax请求
  • 当我没有阅读这部分错误时,我是盲目的:“因此不允许访问 Origin 'localhost'。”。我必须先在线发布,然后再试一次..
猜你喜欢
  • 2014-07-28
  • 1970-01-01
  • 2021-07-02
  • 2014-01-03
  • 1970-01-01
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多