AJAX PUT请求执行POST

时间:2016-02-03 11:49:31

标签: javascript jquery ajax

我有一个动态生成的页面,其内容是通过$ .getJSON()从GET Rest请求中收集的,这部分没问题。

但是,当我运行Ajax请求时,出于某种原因,它会发布一个帖子而不是PUT,它会将页面重新加载到端点URL,而它实际上并没有执行put请求(因为它发布了它)。

为什么会这样?我已经尝试过几乎所有我能想到的东西/我可以在SO&类似。

以下是JS的片段,用于动态生成页面以及AJAX请求。



$("form#submit-unfinished-user-subscription-type").submit(function(e) {
  e.preventDefault(); //STOP default action

  var $this = $(this),
    $formURL = $this.attr("action"),
    $postData = $this.serializeArray();

  $.ajax({
    type: "PUT",
    data: $postData,
    url: $formURL,
    dataType: 'json',
    contentType: "application/json;",
    success: function(data, textStatus, jqXHR) {
      alert("info successfully updated!");
    },
    error: function(jqXHR, textStatus, errorThrown) {
      console.log(jqXHR, textStatus, errorThrown);
    }
  });

  return false;
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
  document.addEventListener("DOMContentLoaded", function(event) {
    var whereTo = $("#records-insert"),
      template;

    $.getJSON("endpoint_here", function(data) {
      for (var i = data.length - 1; i >= 0; i--) {
        template = "<tr><td>" + data[i].forename + " " + data[i].surname + "</td><td>" + data[i].email + "</td><td><form id=\"submit-unfinished-user-subscription-type\" method=\"post\" action=\"endpoint_here\"><input type=\"hidden\" name=\"user_id\" value=\"" + data[i].id + "\"><input type=\"hidden\" name=\"id\" value=\"" + data[i].user_sub_id + "\"><select name=\"subtype_id\"></select><input type=\"submit\"></form></td></tr>";
        whereTo.append(template);
      }
    });

    $.getJSON("endpoint_here", function(data) {
      whereTo = $("select[name=\"subtype_id\"]");
      for (var i = 0; i < data.length; i++) {
        template = "<option value=\"" + data[i].id + "\">" + data[i].name + "</option>";
        whereTo.append(template);
      }

    });
  });
</script>
&#13;
&#13;
&#13;

0 个答案:

没有答案
相关问题