📜  ajax 转换为 js - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:51.100000             🧑  作者: Mango

代码示例2
$("#ajaxForm").submit(function(e){
  e.preventDefault();
  var action = $(this).attr("action");
  var data = {};
  $(this).serializeArray().map(function(x){data[x.name] = x.value;}); 
  $.ajax({
    type: "POST",
    url: action,
    data: JSON.stringify(data),
    contentType: "application/json",
    headers: {
      "Accept": "application/json"
    }
  }).done(function() {
     $('.success').addClass('is-active');
  }).fail(function() {
     alert('An error occurred please try again later.')
  });
});