使用jqueryajax解析restapi

使用jqueryajax解析restapi,jquery,ajax,api,rest,parse-platform,Jquery,Ajax,Api,Rest,Parse Platform,我与parse.com的RESTAPI斗争了一个小时,但没有成功。我不断收到HTTP 401,响应为{“error”:“unauthorized”} 这是我的云代码: Parse.Cloud.define("sendEmail", function(request, response) { var Mailgun = require('mailgun'); Mailgun.initialize('mg.crawford.works', 'key-c67f7a53cf12b2aabeaa

我与parse.com的RESTAPI斗争了一个小时,但没有成功。我不断收到HTTP 401,响应为
{“error”:“unauthorized”}

这是我的云代码:

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");
    }
  });
});
这是我的客户端代码(仅用于表单提交):

我看过很多类似这样的解析支持Fourm(StackOverFlow不允许我在b/c上放置更多链接,我仍然不知道):

感谢你能给予的任何帮助


@所以这段代码最终起作用了,我不知道为什么:

$.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);
                }
            });
$.ajax(“https://api.parse.com/1/functions/sendEmail", {
数据类型:“json”,
方法:'post',
contentType:“应用程序/json”,
数据:JSON.stringify(数据),
标题:{'X-Parse-Application-Id':'sameKey',
“X-Parse-REST-API-Key”:“samekeyreath”
},

成功:函数(数据){/调用云函数时,您应该在URL中使用https而不是http来获取文件,使用http

我复制了您的jQuery ajax调用,并替换为我的应用程序和Rest api键,一切正常。尝试执行尽可能简单的函数,如:
Parse.cloud.define(“hello”,函数(请求,响应){response.success(“Hello world!”);});
使用邮递员一切正常,所以我不再认为是解析造成的。我将发布我的工作代码(我看不出有什么不同)。我剪切并粘贴了我的密钥,所以我不确定为什么会有不同。
$.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);
                }
            });