我和parse.com的REST斗争了一个小时,但没有成功。我一直使用响应{"error": "unauthorized"}获得HTTP401。
这是我的云代码:
Parse.Cloud.define("sendEmail", function(request, response) {
var Mailgun = require('mailgun');
Mailgun.initialize('mg.crawford.works', 'key-c67f7a53cf12b2aabeaade0e50d57e8f');
Mailgun.sendEmail
({
"to": "sales@crawford.works",
"from": "website@crawford.works",
"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我看过很多解析支持,像这样的支持(StackOverFlow不让我放更多的链接b/c,我还是新手):https://www.parse.com/questions/401-unauthorized-error-with-parse-rest-api
感谢你能给予的帮助,
@acrawly
发布于 2015-06-10 14:45:47
调用云函数时,您应该在URL中使用https而不是http,但是要获取文件,请使用http
发布于 2015-06-10 12:36:11
因此,这段代码最终起作用了,我不知道为什么:
$.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。
https://stackoverflow.com/questions/30742833
复制相似问题