2015-08-13 04:01
浏览 116


I am learning AJAX by following some online tutorials and getting help from the very kind SO community when stuck. I am slowly getting the hang of it.

My question is based on 2 different tutorials I am following for a contact form

Tutorial 1 does the follwoing:

        url: "contact_mail.php",
        type: "POST",
        error:function (){}

Tutorial 2 does the follwoing:

var formData = $(form).serialize();
            type: 'POST',
            url: $(form).attr('action'),
            data: formData
        .done(function(response) {

My question

Unfortunately both tutorials does not really cover the url and data attributes in the ajax call and this is where my question comes in.

  1. Where tutorial1 physically gets all the form fields i.e data:'userName='+$("#userName").val()+ tutorial2 simply does the following data: formData

Thus am I correct in saying that you do not physically have to get all the form fields like in tutorial1 but rather you can just get the whole form and assign it to a variable and send that as data?

  1. Tutorial 1 hardcodes the url where tutorial2 gets the form and assign the action attribute to the url: $(form).attr('action'),

Thus am I correct in saying that you do not have to hardcode the URL but rather get it from the form and assign the attribute action to the url?

  1. Which example/ tutorial is the better one in terms of best practice?

Thanks in advance

图片转代码服务由CSDN问答提供 功能建议

学习 AJAX通过以下在线教程并获得SO SO的帮助 社区卡住了。 我正在慢慢掌握它。


< em>教程1执行以下操作:

 data:' 的userName = '+ $( “#的userName”)VAL()+' &安培; USEREMAIL = '+ $( “#USEREMAIL”)VAL()+' &安培受试者='+ $( “#主题”)。  val()+'&amp; content ='+ $(content).val(),
 $(“#mail-status”)。html  (数据); 

教程2执行以下操作 :

  var formData = $(form).serialize(); 
 $ .ajax({

不幸的是,这两个教程都没有涵盖 url data < ajax调用中的/ code>属性,这就是我的问题所在。

  1. 其中tutorial1在物理上获取所有表单字段,即 data:'userName = '+ $(“#userName”)。val()+ tutorial2只执行以下数据:formData


    1. 教程1对 url 进行硬编码,其中tutorial2获取表单并将action属性分配给 url:$(form).attr(' 动作')

      因此,我说你不必对URL进行硬编码,而是从表单中获取并指定 属性操作到网址?

      1. 哪个示例/教程在最佳实践方面更好?


  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douti9253 2015-08-13 04:34

    To answer your first question, jQuery's .serialize() will take a given form or individual inputs/selects/etc. and produce a string of URL-encoded parameters to be sent via POST or GET. This is a very handy thing to do when working with forms. Tutorial 1 is effectively doing the same thing as tutorial 2 by manually constructing this string.

    Tutorial 2 seems preferable over Tutorial 1 here, as Tutorial 1 is far more repetitive and, as such, less maintainable and more error prone. However, a mixture of the two may sometimes be needed, as you may sometimes want to send additional data in the request that isn't in the form.

    For your second question, it doesn't really make too much of a difference what you do here, but you'd probably be better off placing the URL in your markup rather than your code, since you'd typically generate URLs when using a typical Web Framework, and it's good practice to treat HTML as templates and JS/CSS as static assets (you wouldn't want to template your JavaScript with URLs).

    However, this situation might be different if you were using a front-end framework like Angular or Ember.

    解决 无用
    打赏 举报
  • 查看更多回答(2条)

相关推荐 更多相似问题