为什么jQuery的ajax调用,只有当我调试的Chrome浏览器的工作? [英] Why does jQuery ajax call only work when I'm debugging in Chrome?

查看:138
本文介绍了为什么jQuery的ajax调用,只有当我调试的Chrome浏览器的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形式简单的AJAX调用提交。 它的工作原理当我调试,即警报弹出,但运行它时,它不工作?

这似乎有点神秘的对我。

  $(函数(){

    $(#signUpForm)。递交(函数(){
        VAR请求= $阿贾克斯({
            网址:PHP / newUser.php
            键入:POST

        });

        request.done(函数(MSG){
            警报(味精);
        });


    });
});
 

解决方案

如果您运行的 .submit()<调用Ajax /一>的表单处理,Ajax请求将触发但随后的形式将继续提交,从而刷新页面,从来没有射击回调函数。

相反,你应该叫事件。preventDefault()时的距离与正常提交表单提交处理函数prevent浏览器结束一个新的HTTP请求。要做到这一点,你需要添加事件作为参数传递给回调函数,像这样的:

  $(#target).submit(函数(事件){
  警报(处理器的.submit()调用。);
  。事件preventDefault();
});
 

它的工作原理,当你调试,因为一切都发生慢,你逐步完成,无需提交表单的浏览器,其原因

I have a simple ajax call from a form submit. It works when I debug, i.e the alert pops up but when running it, it doesn't work?

This seems a bit mysterious to me.

$(function(){

    $("#signUpForm").submit(function(){
        var request = $.ajax({
            url:"php/newUser.php",
            type:"POST"

        });

        request.done(function(msg){
            alert(msg);
        });


    });
});

解决方案

If you run an ajax call on the .submit() handler of a form, the Ajax request will fire but then the form will continue submitting, thus refreshing the page and never firing your callback function.

Instead, you should call event.preventDefault() at the end of the submit handler function to prevent the browser from submitting the form normally with a new HTTP request. To do this you'll need to add event as a parameter to the callback function, like this:

$( "#target" ).submit(function( event ) {
  alert( "Handler for .submit() called." );
  event.preventDefault();
});

The reason it works when you debug is because everything happens slowly as you step through, without the browser submitting the form.

这篇关于为什么jQuery的ajax调用,只有当我调试的Chrome浏览器的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆