2015-04-29 56 views
0

我试图使用mailjet(由于发送的电子邮件仍然'排队',并且永远不会到达框中,所以mandrill测试不成功)使用AJAX请求我的web应用程序的邮件服务:向mailjet发送ajax请求时无法摆脱多源错误

   $.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': '[email protected]', 
        '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。

+0

望着文档提供http://dev.mailjet.com/ - 我不认为API可以在客户端使用javascript – Filype

+0

含义我认为,你需要使用服务器端技术来与API交谈,这不是不合理的,大多数其他API都会有相同的要求 – Filype

回答

2

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

我想,你需要使用服务器端技术来与API交谈,这并非不合理,其他大多数API都会有相同的要求。

如果你正在寻求建立mailjet,他们已经写了一个PHP包装它https://github.com/mailjet/mailjet-apiv3-php-simple

+0

他们的技术代理说它但它的安全问题不'对我的应用程序很重要。 – Bardelman

+0

我猜你的代码不工作的原因是因为不支持JavaScript客户端Ajax请求 – Filype

+0

我没看过这部分错误:“Origin'http:// localhost'因此是盲目的不允许访问“。我必须先在线发布它,然后再试一次。 – Bardelman

相关问题