简体   繁体   English

AJAX-“ POST”请求首先执行“ GET”请求,然后执行“ POST”请求

[英]AJAX - “POST” request is performing a “GET” request first, then a “POST” request

I am working on a RoR and JavaScript project and am using an AJAX request to submit a form. 我正在研究RoR和JavaScript项目,并且正在使用AJAX请求提交表单。 I have a POST request on submit, but it is performing a GET request at first. 我在提交时有POST请求,但最初是在执行GET请求。 The form is clearing and the page reloads. 表单正在清除,页面重新加载。

The url changes from http://localhost:3000/beers to http://localhost:3000/beers?utf8=%E2%9C%93&authenticity_token=HVoHQ8WEZdIfVEvBcWJU5XT0dIiV2QL0B%2B2XeaY5m9g69A4yYWFxmx%2B4qiv2qPqx%2BJe3pphvtGT0oZhlWNTw3A%3D%3D&beer%5Bname%5D=Fat+Tire&beer%5Bbrewery_attributes%5D%5Bname%5D=New+Belgium+Brewing+Company&beer%5Bbeer_type%5D=Red+Ale+-+American+Amber+%2F+Red&beer%5Bibu%5D=22&beer%5Babv%5D=5.2%25&commit=Create+Beer . url从http://localhost:3000/beers更改为http://localhost:3000/beers?utf8=%E2%9C%93&authenticity_token=HVoHQ8WEZdIfVEvBcWJU5XT0dIiV2QL0B%2B2XeaY5m9g69A4yYWFxmx%2B4qiv2qPqx%2BJe3pphvtGT0oZhlWNTw3A%3D%3D&beer%5Bname%5D=Fat+Tire&beer%5Bbrewery_attributes%5D%5Bname%5D=New+Belgium+Brewing+Company&beer%5Bbeer_type%5D=Red+Ale+-+American+Amber+%2F+Red&beer%5Bibu%5D=22&beer%5Babv%5D=5.2%25&commit=Create+Beer

I press submit once again (with an empty form) and then it performs the POST request and works as it should. 我再次按Submit(使用空白表格),然后它执行POST请求并按应有的方式工作。 It doesn't do this every single time , which is what is really confusing me. 它不会每次都这样做 ,这确实让我感到困惑。 I have cleared my cookies, restarted my server, and tried a different browser. 我已经清除了Cookie,重新启动了服务器,并尝试了其他浏览器。 None have fixed the issue. 没有人能解决这个问题。 If anyone has any insight, I'd greatly appreciate the help. 如果有人有任何见识,我将非常感谢您的帮助。 Thanks in advance. 提前致谢。

My AJAX request: 我的AJAX请求:

$(function() {
    // new beer request
    $('#new-beer-form').on("submit", function(e) {
        $.ajax({
            url: this.action,
            method: "POST",
            data: $(this).serialize(),
            success: function(response) {
                var $ol = $("div.beers ol")
                $ol.append(response);
            },
            error: function(response) {
                alert("Please fill out all criteria.");
            },
        });
        e.preventDefault();
    })
})

I think that your e.preventDefault() should be moved to the very beginning of the on.Submit function's callback. 我认为您的e.preventDefault()应该移到on.Submit函数回调的最开始。 Before the ajax request. 在ajax请求之前。 I think what is happening is that the default action (sending a get request) is not prevented in time. 我认为发生的事情是没有及时阻止默认操作(发送get请求)。

Can you verify if the button you are using to submit defined as <button> ? 您可以验证用于提交的按钮是否定义为<button>吗? If yes, it should have attribute type="button" , since it will default to "submit" action on click, as being replied here: 如果是的话,它应该具有type="button"属性,因为它将默认为单击时"submit"操作,如下所示:

What's the point of <button type="button">? <button type =“ button”>有什么意义?

So, it appears that the issue lied within my gemfile. 因此,问题似乎在于我的gemfile中。 I was utilizing the turbolinks gem when I shouldn't have been. 我本来应该利用turbolinks gem。 As soon as I went through the necessary steps to remove it from my project, it began working correctly, consistently. 一旦我完成了将其从项目中删除的必要步骤,它便开始正确,持续地工作。

For anyone that has a similar issue, here is a really helpful and straightforward article in regards to the topic: http://codkal.com/rails-how-to-remove-turbolinks/ . 对于有类似问题的任何人,这里都有关于该主题的非常有用且直接的文章: http : //codkal.com/rails-how-to-remove-turbolinks/

Thank you to everyone that helped me. 谢谢所有帮助我的人。

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

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