使用带有AJAX的解析REST API时,代码107无效的JSON

时间:2015-04-21 06:10:22

标签: javascript jquery ajax json parse-platform

我正在尝试使用Parse REST API发送推送通知,但每当我尝试进行AJAX调用时,我都会在响应和400状态代码中找回无效的JSON错误。这是我的要求



$.ajax({
  url: 'https://api.parse.com/1/push',
  headers: {
    "X-Parse-Application-Id": "apID",
    "X-Parse-REST-API-Key": "restKey",
    "Content-Type": "application/json"
  },
  data: {
    "where": {
      "appName": "CampMo"
    },
    "data": {
      "alert": "Test notification"
    }
  },
  error: function(e) {
    console.log(e);
  },
  success: function(data) {
    alert("success!");
  },
  type: 'POST'
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

当我使用像Postman这样的客户端执行此请求时,该请求正常,但在我尝试通过我的Web应用程序执行时,请求无效。有什么我想念的吗?

1 个答案:

答案 0 :(得分:1)

将您的数据作为JSON字符串化数据传递

$.ajax({
  url: 'https://api.parse.com/1/push',
  headers: {
    "X-Parse-Application-Id": "apID",
    "X-Parse-REST-API-Key": "restKey",
    "Content-Type": "application/json"
  },
  data: '{"where": {"appName": "CampMo"},"data":{"alert":"Test notification" }}',
  error: function(e) {
    console.log(e);
  },
  success: function(data) {
    alert("success!");
  },
  type: 'POST'
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>