【问题标题】:Parse REST API 401 Unauthorized with JQuery Ajax使用 JQuery Ajax 解析 REST API 401 未经授权
【发布时间】: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


【解决方案1】:

调用云函数时,您应该在 URL 中使用 https 而不是 http,但要获取文件,请使用 http

【讨论】:

    【解决方案2】:

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

    $.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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      • 2017-04-22
      • 2021-04-07
      相关资源
      最近更新 更多