2015-06-09 60 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。

相關問題