【发布时间】:2015-06-12 00:18:20
【问题描述】:
我已经与 parse.com 的 REST API 斗争了一个小时,但没有成功。我不断收到响应为{"error": "unauthorized"} 的HTTP 401。
这是我的云代码:
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
【问题讨论】:
-
我复制了您的 jQuery ajax 调用并替换为我的 Application 和 Rest api 键,一切正常。尝试执行尽可能简单的函数,如下所示:
Parse.Cloud.define("hello", function(request, response) { response.success("Hello world!"); }); -
使用 Postman 一切正常,所以我不再认为 Parse 是罪魁祸首。我将发布我的工作代码(我看不出有什么不同)。我剪切并粘贴了我的钥匙,所以我不确定为什么会有所作为。
标签: jquery ajax api rest parse-platform