简体   繁体   English

如何确定使用JQuery和Ajax调用哪个函数?

[英]How to decide which function to call with JQuery and Ajax?

I have been working on single page application where users can run different processes. 我一直在开发单页应用程序,用户可以在其中运行不同的进程。 That would involve adding new records, updating existing records and deleting records in some situations. 在某些情况下,这将涉及添加新记录,更新现有记录以及删除记录。 My question is what is the best way to handle this processes on the front end? 我的问题是在前端处理此过程的最佳方法是什么? I have tried to monitor how Stack-overflow works and how they handle this situations. 我试图监视Stack-overflow工作方式以及它们如何处理这种情况。 Still can say 100% that I'm understanding if they use different url or they use submitted form data to decide which process should run on the back end. 仍然可以说100%我了解,如果他们使用不同的url或使用提交的表单数据来决定哪个进程应在后端运行。 Here is small example: 这是一个小例子:

<div class="row">
   <div class="form-group col-xs-12 col-sm-12 col-md-12 col-lg-12">
       <button type="button" name="new_record" id="new_record">New</button>
   </div>
</div>
<table>
   <thead>
      <tr>
         <th>Name</th>
         <th>Status</th>
         <th>Edit</th>
      </tr>
   </thead>
   <tbody>
      <tr>
        <td>New York</td>
        <td>Active</td>
        <td class="text-center">
           <button class="record_edit">Edit</button>
        </td>
      </tr>
   </tbody>
</table>

In HTML above you can see two different buttons New and Edit . 在上面的HTML中,您可以看到两个不同的按钮NewEdit I only showed one record in table for testing purpose. 我只在表中显示了一条记录用于测试。 So this si situation where I'm wondering how to handle this in JQuery/JavaScript and Ajax. 所以在这种情况下,我想知道如何在JQuery / JavaScript和Ajax中处理它。 Here is example of my js code: 这是我的js代码示例:

$('#frmSave').on('submit', submitFrm);
function submitFrm(e){
    var formData = frmObject.serialize();

    $.ajax({
       type: 'POST',
       url: 'Components/Functions.cfc?method=saveData',
       data: formData,
       dataType: 'json'
    }).done(function(obj){
       if(obj.STATUS === 200){
           // Successfully saved.
       }else{
           // Error!   
       }  
    }).fail(function(jqXHR, textStatus, errorThrown){
       alert('Error: '+errorThrown);
    });
}

You can see that I call one function for both cases New and Edit. 您可以看到,我在“新建”和“编辑”两种情况下都调用了一个函数。 On the back end I have two different processes. 在后端,我有两个不同的过程。 One it's for insert and other for update. 一种用于插入,另一种用于更新。 Based on parameters passed from the user that will be decided on the back end. 基于从用户传递的参数,这些参数将在后端确定。 How ever I know that I can use data-attributes and append that value to my formData with url name and send to the server. 我怎么知道我可以使用data-attributes并将该值附加到具有url名称的formData并发送到服务器。 Some people will say that is not reliable.My code on the back end will Insert or Update based on the submitted parameters in form data. 有人会说这是不可靠的,我后端的代码将根据表单数据中提交的参数进行插入或更新。 I'm wondering what is a good way for this situation now days. 我想知道如今对于这种情况有什么好的方法。 How that is handled in professional manner? 如何以专业的方式处理? If anyone have a good example please let me know. 如果有人有好的榜样,请告诉我。 One more thing, I would like to keep these two processes in the same function on the back end to prevent some code redundancy in my opinion. 还有一件事,我想将这两个进程保留在后端的同一功能中,以防止某些代码冗余。

You could do something as simple as 您可以做一些简单的事情

.done(function(obj){
       if(obj.STATUS === 200){
           // Successfully saved.
            $('#main').load("./newContent.cfm");
          }

So on success, it loads the newContent page. 因此,一旦成功,它将加载newContent页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM