在向mailjet发送ajax请求时无法消除多源错误

时间:2015-04-29 09:19:27

标签: javascript ajax mailjet

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

             $.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发送电子邮件。

1 个答案:

答案 0 :(得分:2)

查看http://dev.mailjet.com中提供的文档 - 我不认为API可以在客户端javascript中使用。

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

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