在jQuery中调用getJSON后提交表单

时间:2014-06-18 09:09:34

标签: jquery html ajax forms getjson

我有以下表格:

$('#ambition_submit').click(function(e){
    e.preventDefault();
    var output = getTimelineEntryData(data.toJSON());//console.log(output);
    $.getJSON('http://www.x.com/x/api/add_subgoals.php',{action:'add.subgoals',ambition_id:ambition_id,subgoals:output}, function (response) {
        //var this_url = this.url; console.log(this_url);
        if (response.status == 200) {
            var result = response.message;//alert(response.message);
            $('#form_content').submit();
            $('form[name=testing747]').submit();//console.log('Test ' + $('form[name=testing747]').length);
        }
        else{
            alert(response.message);
        }
    });
});

在提交时,我希望ajax运行并在完成时提交表单。如何让表格明确提交ajax的结果?

由于

1 个答案:

答案 0 :(得分:2)

所以这就是问题:

  

如何让表格明确提交ajax的结果?

为此,您可以使用适用于 .always() 方法的both success and error链接您的ajax,如:

  $('#ambition_submit').click(function (e) {
      e.preventDefault();
      var output = getTimelineEntryData(data.toJSON()); //console.log(output);
      $.getJSON('http://www.x.com/x/api/add_subgoals.php', {
          action: 'add.subgoals',
          ambition_id: ambition_id,
          subgoals: output
      }, function (response) {
          //var this_url = this.url; console.log(this_url);
          if (response.status == 200) {
              var result = response.message; //alert(response.message);
          } else {
              alert(response.message);
          }
      }).always(function () {
          $('#form_content').submit();
          $('form[name=testing747]').submit(); //console.log('Test ' + $('form[name=testing747]').length);
      });
  });