【问题标题】:Load form via ajax then send form via Ajax?通过 ajax 加载表单然后通过 Ajax 发送表单?
【发布时间】:2013-12-22 00:50:27
【问题描述】:

好的,我正在通过 ajax 调用将表单加载到页面中,如下所示:

    $('.ajax_click').on('click', function(event) {
  event.preventDefault();
  /* Act on the event */
  var getmsgtoload = $(this).find('.ajax_get_msg').attr('href');
  var getpelytoload = $(this).find('.ajax_get_reply').attr('href');  
  //get reply
  $.ajax({
    url: getpelytoload,
  }).done(function(data) {
    var getreply = $(data).find('#reply_div').html();
    $('#reply_div').replaceWith('<div id="reply_div">'+getreply+'</div>');
          //Process our send!
          $('#create').submit(function() { // catch the form's submit event
             $.ajax({ // create an AJAX call...
                    data: $(this).serialize(), // get the form data
                    type: $(this).attr('method'), // GET or POST
                    url: $(this).attr('action'), // the file to call
                })
                  .done(function(data){ // the file to call
                  console.log(data);
                      $('#created').html('<h1>Success</h1>'); // update the DIV
                  })
                  .fail(function(){
                    console.log('error on the from....');
                  });
              return false; // cancel original event to prevent form submitting
          });
  })
  .fail(function() {
    console.log("error");
  })
  .always(function() {
    console.log("complete");
  });
});

我也尝试通过 ajax 从表单发送数据,但 data 参数只返回整个页面而不仅仅是表单。 为什么? 克里斯 澄清 该表单根本不会“发布”数据,但它确实在我从中拉出表单的页面上工作,如下所示: 我在 ajax 的回调成功函数中使用从页面 A 到页面 B 的 ajax 调用将表单调用到 DIV A 然后我运行 ajax 以通过序列化发送表单数据,但在查看表单时它不会发送在页面 A 上它工作正常,但当它被拉入页面 B 时就不行了! 克里斯

【问题讨论】:

  • $(this).serialize() 输出什么?当我通过 ajax 从表单提交数据时,我通常最终会获取每个字段值并为每个字段设置一个变量。
  • 这可能会有所帮助stackoverflow.com/questions/9557761/…
  • 这个序列化在嵌套的 ajax 输出数据之前很好。成功的数据输出整个页面...
  • @MickeySlater 确实在一定程度上有所帮助,但我已经将处理程序绑定到第一个 ajax 的成功函数上......

标签: javascript jquery ajax


【解决方案1】:

因为在您的“完成”事件处理程序中,您会收到服务器对您的 POST 的响应。在您的情况下,服务器会响应整个页面。

【讨论】:

  • 现在这很有意义......所以我如何发送表单上设置的方法是 POST 并且它在独立页面上时有效..
  • 您能否进一步澄清您的问题?我不太明白。
猜你喜欢
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-06
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多