向Mailjet发送Ajax请求时无法摆脱多源错误 [英] Couldn't get rid of the multi-origins error when sending an ajax request to mailjet

查看:67
本文介绍了向Mailjet发送Ajax请求时无法摆脱多源错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AJAX请求在我的Web应用程序中使用mailjet(由于进行的mandrill测试未成功,因为发送的电子邮件仍然处于排队"状态,并且从未到达邮箱):

I'm trying to use the mailjet(as mandrill tests weren't successfuls because sent email remained 'queued' and never reach the box) mailing service in my web application using an AJAX request :

             $.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
                 });
        }

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

as i got this error in the browsers consoles :

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服务器,然后直接在浏览器中启动了该应用程序.

I tried to find a solution to get rid of it and i found two solution : 1/ using CORS. 2/ i also found that it can be a problem to send the request from a local web page so i runned a wamp server and i launched the app on it instead directly in browsers.

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

But i still have that error :/ Any suggestion ? Next i will try the google gmail API for sending email.

推荐答案

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

Looking at the documentation available in http://dev.mailjet.com - I don't think the API can be used in client-side javascript.

我认为,您需要使用服务器端技术来与API通讯,这并不是不合理的,其他大多数API也会有相同的要求.

I think, you need to use a server side technology to talk to the API, that's not unreasonable most other APIs will have the same requirement.

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

If you're looking to set up mailjet, they have have written a php wrapper for it https://github.com/mailjet/mailjet-apiv3-php-simple

这篇关于向Mailjet发送Ajax请求时无法摆脱多源错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆