JSON发布到API

时间:2015-05-19 01:32:56

标签: javascript ajax api

我正在尝试为Simpleconsign API创建一个JSON帖子,并且我一直从浏览器收到0错误。这是我的代码:

<script type="text/javascript">

JSONTest = function() {
    var resultDiv = $("#resultDivContainer");
    $.ajax({
        type: 'POST',
         key: "apikey,
        url: 'https://user.traxia.com/app/api/inventory', 
        contentType: "application/json",
         success: function(result){
            switch (result) {
                case true:
                    processResponse(result);
                    break;
                default:
                    resultDiv.html(result);
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
        }
    });
};

</script>

我是发布到REST API的初学者,所以任何帮助都会非常感激。谢谢!

1 个答案:

答案 0 :(得分:1)

我在Ajax POST中看不到任何数据?这段代码应该有效

var postData = {
  "key": "Your API Key Here",
  "query": "some query",
  "consignorId": "123456",
  "includeItemsWithQuantityZero": "false"
};

JSONTest = function() {
  var resultDiv = $("#resultDivContainer");
  $.ajax({
    type: 'POST',
    data: postData,
    url: 'https://user.traxia.com/app/api/inventory',
    contentType: "application/json",
    success: function(result) {
      switch (result) {
        case true:
          processResponse(result);
          break;
        default:
          resultDiv.html(result);
      }
    },
    error: function(xhr, ajaxOptions, thrownError) {
      alert(xhr.status);
      alert(thrownError);
    }
  });
};

http://wiki.traxia.com/display/guide/List+and+Search+Inventory