2015-06-09 68 views
-1

我一直在与parse.com的REST API争斗一个小时,但没有成功。我不断收到HTTP 401响应{"error": "unauthorized"}解析REST API 401未经授权使用JQuery Ajax

这里是我的云代码:

Parse.Cloud.define("sendEmail", function(request, response) { 

    var Mailgun = require('mailgun'); 
    Mailgun.initialize('mg.crawford.works', 'key-c67f7a53cf12b2aabeaade0e50d57e8f'); 

    Mailgun.sendEmail 
    ({ 
    "to": "[email protected]", 
    "from": "[email protected]", 
    "subject": "Website Form Submission", 
    "text": "Name: " + request.params.name + "\nEmail: "+request.params.email+"\nPhone: "+request.params.phone+"\nMessage: "+request.params.msg 
    }, 
    { 
    success: function(httpResponse) 
    { 
     console.log(httpResponse); 
     response.success("success"); 
    }, 
    error: function(httpResponse) 
    { 
     console.error(httpResponse); 
     response.error("error"); 
    } 
    }); 
}); 

这里是(刚刚提交表单)我的客户端代码:

var data = {}; 

data.name = $("#name").val(); 

data.email = $("#email").val(); 

data.msg = $("#message").val(); 

data.phone = $("#phone").val(); 
$.ajax({ 
    method: 'post', 
    url: "http://api.parse.com/1/functions/sendEmail", 
    data: JSON.stringify(data), 
    contentType: 'application/json', 
    headers: 
    { 
    'X-Parse-Application-Id': 'This is the right key, triple checked', 
    'X-Parse-REST-API-Key': 'Same story here' 
    } 
}) 
.done(function (response) { 
    if (response.success == 'success') {    
    alert('success');      
    } else { 
    alert('fail'); 
    } 
}); 
return false; // required to block normal submit since you used ajax 

我看过像这样的很多解析的支持fourms( StackOverFlow是不让我把更多的链接B/C我仍然noob):​​

欣赏任何帮助,你可以给,

@acrawly

+0

我复制你的jQuery ajax调用和我的应用程序和REST API键取代,一切工作就好了。尝试执行尽可能简单的功能,例如:'Parse.Cloud.define(“hello”,function(request,response){ response.success(“Hello world!”); });' –

+0

使用Postman工作正常,所以我从思想转移到解析是怪。我会发布我的工作代码(我没有看到区别)。我剪下并粘贴了我的钥匙,所以我不确定为什么会有所作为。 –

回答

0

当调用云功能,您应该在HTTPS URL不是HTTP,但获取的文件,使用http

0

所以这段代码结束了工作,我想不通为什么:

$.ajax("https://api.parse.com/1/functions/sendEmail", { 
       dataType: 'json', 
       method:'post', 
       contentType: "application/json", 
       data: JSON.stringify(data), 
       headers: { 'X-Parse-Application-Id': 'sameKey', 
         'X-Parse-REST-API-Key': 'sameKeyAgain' 
       }, 
       success: function(data) { //<-- I thought maybe success vs done was it but 
        //I changed this out and no diff in the result 

        if(data.result === 'success') 
        { 
         alert("success"); 
        } 
        else 
        { 
         alert("error"); 
        } 

       }, 
       error: function(data) { 
        alert(data); 
       } 
      }); 

编辑:由于@hasen指出下面,那是因为我没有使用HTTPS。

相关问题