ajax请求每秒返回10个响应 [英] ajax request returns 10 responses per second

查看:95
本文介绍了ajax请求每秒返回10个响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

取决于ajax返回什么(1或0),我希望是否处理提交动作:

Depending on what ajax returns ( 1 or 0 ), I want the submit-action to be processed or not:

$(document).ready(function(){
  $("#submit").submit(function(e){

    e.preventDefault();
    var username = $("#username").val();
    if(username){
        $.ajax({
            url: 'checkinput.php',
            data: {data: JSON.stringify(username)},
            type: 'POST',
            dataType: "json",
            success: function (data) {
                if(data.result == 0) {
                    alertify.log( data.error ); // diplay error
                }
                if(data.result == 1) {
                    $("#submit").submit(); // process submit-action => send data to php etc..
                }
            }
        });
    } else {
        alertify.log( "forgotten something" );
    }    
  });
}); 

如果php脚本返回的结果为0,则显示错误消息并且页面没有刷新(GREAT)< =应该是这样.

If the returned result from the php-script is 0, than the error-message is displayed and the page is not refreshed ( GREAT ) <= It should be like this.

如果Ajax返回1,我希望处理提交动作,以便可以将数据发送到php. 但是会发生什么:

If Ajax returns 1, I want the submit action to be processed, so data can be send to php. But what happens:

Ajax每秒发送10个响应(可能是由于我在if子句中调用的功能?) 如果返回的结果是1,我希望处理该提交动作. 该如何解决?

Ajax sends 10 responses per second (maybe because of the function I call in the if-clause ? ) If the returned result is 1, I want the submit-action to be processed. How to solve this?

推荐答案

您有一个无限循环,因为$("#submit").submit()触发了提交event.您想要的是提交表单而不触发事件.幸运的是,这仅意味着在代码[0]中添加3个字符.

You have an infinite loop because $("#submit").submit() triggers the submit event. what you want instead is to submit the form without triggering the event. Lucky for you, that just means adding 3 characters to your code, [0].

$("#submit")[0].submit();

这篇关于ajax请求每秒返回10个响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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