jQuery .ajax 不起作用,通过页面刷新形成帖子

发布于 2024-12-04 21:44:50 字数 1251 浏览 0 评论 0原文

我有以下 html 代码:(通过 ajax 加载到 div 中)

(剪掉不相关的部分)

<form id='addContactForm' method='post' action='/contact/create/'>
    <div>
        <input type="image" name="submit" id="addContactSubmit" src="/images/add-small.png" style="width: auto; vertical-align: bottom;">
    </div>
</form>

<script>
    $('#addContactSubmit').click(function () {
        alert('got here');
        $.ajax({
            type: 'POST',
            url: '/contact/create/',
            data: $("#addContactForm").serialize(),
            success: function (data, status, xhttp) {
                if (data) {
                    alert('Got Data');
                } else {
                    alert('No Data');
                }
            },
            dataType: html
        });
        return false;
    });
</script>

所以现在当我单击 时,它实际上会发布表单并刷新页面。我读过另一篇文章,指出问题是 jquery 未加载,一旦加载就解决了问题。然而,在这种情况下,我收到警报,它告诉我 jquery 正在加载(因为单击事件触发正常),但是似乎 $.ajax 部分没有被执行(或者有问题)。如果我删除 $.ajax 部分,我会收到警报,并且页面不会提交和重新加载。

最初我说这个表单 html 是通过主页上的 ajax 调用加载的,我想知道这是否与失败的 ajax 帖子有关。请注意,我也从未收到警报(“有数据”)或(“无数据”),只有警报“到达这里”

任何现场都会很棒。谢谢

另一条注释,我在萤火虫中没有收到任何错误。

I have the following html code: (that gets loaded via ajax into a div)

(Cut out the irrelevant parts)

<form id='addContactForm' method='post' action='/contact/create/'>
    <div>
        <input type="image" name="submit" id="addContactSubmit" src="/images/add-small.png" style="width: auto; vertical-align: bottom;">
    </div>
</form>

<script>
    $('#addContactSubmit').click(function () {
        alert('got here');
        $.ajax({
            type: 'POST',
            url: '/contact/create/',
            data: $("#addContactForm").serialize(),
            success: function (data, status, xhttp) {
                if (data) {
                    alert('Got Data');
                } else {
                    alert('No Data');
                }
            },
            dataType: html
        });
        return false;
    });
</script>

So now when I click on the <image>, it actually posts the form and refreshes the page. I read another post that stated that the problem was that jquery wasn't loaded, and once loaded that fixed the problem. However, in this case, I am getting the alert, which tells me that jquery is loading (as the click event is triggering fine), however it seems the $.ajax portion is not being executed (or there is a problem with it). If I remove the $.ajax portion, I get the alert, and the page does not submit and reload.

Originally I stated that this form html gets loaded via an ajax call on the main page, and I am wondering if that has something to do with the failing ajax post. Note I also never get the alert ('Got Data') or ('No Data'), only the alert 'got here'

any insite would be great. Thanks

Another note, I am getting no errors in firebug.

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

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

发布评论

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

评论(1

独木成林 2024-12-11 21:44:50

这是一个提交输入,它将自动回发表单并运行您的 ajax 脚本。

试试这个...

<script>
    $('#addContactSubmit').click(function (e) {
        e.preventDefault();
        alert('got here');
        $.ajax({
            type: 'POST',
            url: '/contact/create/',
            data: $("#addContactForm").serialize(),
            success: function (data, status, xhttp) {
                if (data) {
                    alert('Got Data');
                } else {
                    alert('No Data');
                }
            },
            dataType: html
        });
        return false;
    });
</script>

我所做的就是将单击事件传递给您的 jQuery 函数,然后调用 e.preventDefault() 停止执行默认的 DOM 单击事件,在本例中,提交按钮是回发。

希望这是有道理的,你说得非常对!

It's a submit input which will automatically post back the form as well as run your ajax script.

Try this...

<script>
    $('#addContactSubmit').click(function (e) {
        e.preventDefault();
        alert('got here');
        $.ajax({
            type: 'POST',
            url: '/contact/create/',
            data: $("#addContactForm").serialize(),
            success: function (data, status, xhttp) {
                if (data) {
                    alert('Got Data');
                } else {
                    alert('No Data');
                }
            },
            dataType: html
        });
        return false;
    });
</script>

All I've done is pass the click event to your jQuery function then calling e.preventDefault() stops the execution of the default DOM click event which in this case for a submit button is a post back.

Hope this makes sense, you had it pretty much right!

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