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

发布于 2025-01-06 03:11:37 字数 887 浏览 4 评论 0原文

我在网上浏览如何将 AJAX 与 Stripes 结合使用,并找到了这个。然而,文档解释了使用原型框架作为客户端脚本。不是 JavaScript。但我想用javascript代替。

我认为这个块使用了原型库。

<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>

我怎样才能将其更改为javascript?或者我误解了什么。 我的主要目标是,当用户单击按钮时,我想将该数据发送到服务器并将其显示在表格上,而不刷新整个页面。我有一个 ActionBean 来保存数据。

谢谢。

I was surfing the web how to use AJAX with Stripes and found this. However the documentation explains using Prototype framework as a client side script. Not Javascript. But I want to use javascript instead.

I think this block uses Prototype library.

<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? 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.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鯉魚旗 2025-01-13 03:11:37

如果您不打算导入库(我建议这样做,因为此代码对于非 JS 程序员来说可能有点繁重),那么您需要自己构建 XMLHttpRequest 对象。 MDN 上有一个很好的指南,涵盖了基础知识,但通常是这样做的so:

// 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();

如您所见,这不是最简单的代码。我强烈建议您仔细阅读有关该主题的 MDN 页面,因为它会对您有所帮助理解上面的代码。它可以帮助您执行更多操作,例如随请求一起发送数据。并不是说这里的序列化和提交表单比我上面的示例更多。如果您想提交表格,您需要确保彻底阅读该文章。

我为什么推荐像 Prototype 或 jQuery 这样的 JS 库,因为它们使所有这些(以及更多)变得更加简单。

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. There is a good guide over at MDN that covers the basics, but it is generally done like so:

// 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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文