简体   繁体   English

Stripes和Ajax-如何使用AJAX将数据发送到服务器并使用数据刷新表

[英]Stripes and Ajax- How do i send data to a server and refresh a table with data using AJAX

I was surfing the web how to use AJAX with Stripes and found this . 我在网上冲浪如何将AJAX与Stripes结合使用,发现了这一点 However the documentation explains using Prototype framework as a client side script. 但是,文档说明了使用Prototype框架作为客户端脚本。 Not Javascript. 不是Javascript。 But I want to use javascript instead. 但我想改用javascript。

I think this block uses Prototype library. 我认为该块使用Prototype库。

<script type="text/javascript"
              src="${pageContext.request.contextPath}/ajax/prototype.js"></script>
      <script type="text/javascript" xml:space="preserve">
          /* ... */
          function invoke(form, event, container) {
              if (!form.onsubmit) { form.onsubmit = function() { return false } };
              var params = Form.serialize(form, {submit:event});
              new Ajax.Updater(container, form.action, {method:'post', parameters:params});
          }
      </script>

How can I change it to javascript? 如何将其更改为javascript? Or did I misunderstood something. 还是我误会了一些东西。 My main aim is when a user clicks a button I want to send that data to the server and display it on a table without refreshing the whole page. 我的主要目的是当用户单击一个按钮时,我想将该数据发送到服务器并在不刷新整个页面的情况下将其显示在表上。 I have an ActionBean who will save the data. 我有一个ActionBean,它将保存数据。

Thank you. 谢谢。

If you are not going to import a library (which I would recommend, because this code can be a little heavy for non-JS programmers) then you'll need to build the XMLHttpRequest object yourself. 如果您不打算导入库(我建议这样做,因为该代码对于非JS程序员可能会有点沉重),那么您将需要自己构建XMLHttpRequest对象。 There is a good guide over at MDN that covers the basics, but it is generally done like so: MDN上有一个很好的指南 ,涵盖了基础知识,但通常这样做是这样的:

// Build the object
var httpRequest;  
if (window.XMLHttpRequest) { // Mozilla, Safari, ...  
    httpRequest = new XMLHttpRequest();  
} else if (window.ActiveXObject) { // IE 8 and older  
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");  
}
// Add the function for handling changes to state
httpRequest.onreadystatechange = function () {
    // Check the state of the request
    if (httpRequest.readyState === 4) {
        // The server has finished sending data and we are ready to handle it
        if(httpRequest.status < 400) {
            // Request was successful, Put your code for handling the response here
            // String of what the response was will be in httpRequest.responseText
        } else {
            // Request was not successful, probably want to display an error
        }
    } else {
        // Response is still being received and is not quite ready yet.
    }
}
// Now that we can handle it, we want to send the request
httpRequest.open("GET", "http://example.com");
// Don't forget to send it :)
httpRequest.send();

As you can see, it isn't the easiest code. 如您所见,它不是最简单的代码。 I'd highly recommend reading through the MDN page on the subject , because it will help you understand the code above. 我强烈建议您通读有关该主题MDN页面 ,因为它将帮助您理解上面的代码。 It can help you do more things with this, like send data along with your request. 它可以帮助您执行更多操作,例如与请求一起发送数据。 Not that serializing and submitting a form here is more than my example above does. 并不是说在这里序列化和提交表单比上面的示例更多。 If you want to submit a form, you'll want to make sure to read through the article thoroughly. 如果要提交表单,则需要确保通读全文。

Why I recommend a JS library like Prototype or jQuery is because they make all of this (and more) a lot simpler. 我之所以推荐像Prototype或jQuery这样的JS库的原因是因为它们使所有这些(以及更多)变得更加简单。

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

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