Jquery Ajax可以一次接一次地工作

Jquery Ajax可以一次接一次地工作,jquery,ajax,jquery-mobile,Jquery,Ajax,Jquery Mobile,我的AJAX调用有问题。我有一个包含“showuserinfo”类按钮的列表,当我点击showuserinfo并返回并再次点击时,AJAX调用不止一次起作用。如果我点击showuserinfo,返回并再次点击,AJAX调用将工作两次。如果我反复这么做,AJAX调用也会反复运行。这是我的密码: $('.showuserinfo').live('tap', function(event){ $.mobile.loading('show'); var user_id

我的AJAX调用有问题。我有一个包含“showuserinfo”类按钮的列表,当我点击showuserinfo并返回并再次点击时,AJAX调用不止一次起作用。如果我点击showuserinfo,返回并再次点击,AJAX调用将工作两次。如果我反复这么做,AJAX调用也会反复运行。这是我的密码:

 $('.showuserinfo').live('tap', function(event){
        $.mobile.loading('show');
        var user_id = $(this).children().find('a').attr('id');
        //$('#showprofile').page('destroy').page();
        $('#showprofileImage img').remove();
        $('#showprofileContent div').remove();
        $( document ).delegate("#showprofile", "pagebeforeshow", function() {
         $.ajax({
            type: "POST",
            url: "http://fitness.desigyn.com/func/phonegapApi.php",
            data: { operation: "getSpecificProfile", requested: user_id ,username: 'kurekcifuat', password: '123456' },
            async : false,
            cache : false,
            }).done(function( msg ) {
                if(msg == "fail")
                {
                    alert('error');
                }
                else{
                    var obj = $.parseJSON(msg);
                        alert(msg); // Debugging and that code works more than once
                    $('#showprofileImage img').remove();
                    $('#showprofileContent div').remove();
                    $('#showprofileImage').append('<img class="otheruserpic" style="height:120px !important; width:120px !important;" src="' + obj["0"].picture + '"/>');
                    $('#showprofileContent').append('<div class="otheruserinfo"><b>Username: </b><span>' + obj["0"].username + '</span></div><div><b>Name: </b><span>' + obj["0"].name + '</span></div><div><b>Age: </b><span>' + obj["0"].age + '</span></div><div><b>Gender: </b><span>' + obj["0"].gender + '</span></div>');
                }
            });
            $.mobile.loading('hide');
         });

        });
$('.showuserinfo').live('tap',函数(事件){
$.mobile.load('show');
var user_id=$(this.children().find('a').attr('id');
//$('#showprofile').page('destroy').page();
$(“#showprofileImage img”).remove();
$('#showprofileContent div')。删除();
$(document).delegate(“#showprofile”,“pagebeforeshow”,function()){
$.ajax({
类型:“POST”,
url:“http://fitness.desigyn.com/func/phonegapApi.php",
数据:{操作:“getSpecificProfile”,请求:用户id,用户名:'kurekcifuat',密码:'123456'},
async:false,
cache:false,
}).done(函数(msg){
如果(消息==“失败”)
{
警报(“错误”);
}
否则{
var obj=$.parseJSON(msg);
alert(msg);//调试,并且该代码可以多次工作
$(“#showprofileImage img”).remove();
$('#showprofileContent div')。删除();
$('#showprofileImage')。附加('');
$(“#showprofileContent”).append('Username:'+obj[“0”]。Username+'Name:'+obj[“0”]。Name+'Age:'+obj[“0”]。Age+'Gender:'+obj[“0”]。Gender+';
}
});
$.mobile.load('hide');
});
});
有什么建议吗?
干杯。

多事件绑定有问题

更改此项:

$('.showuserinfo').live('tap', function(event){

});
为此:

$('.showuserinfo').die('tap').live('tap', function(event){

});
或者更好地用于以下情况:

$(document).off('tap', '.showuserinfo').on('tap', '.showuserinfo' ,function(event){

});

阅读有关多事件绑定的更多信息,搜索章节:防止多事件绑定/触发

首先,使用
。jQuery Mobile不建议使用ready()
。其次,
.live()
被替换为
.on()

当触发
pagebeforeshow
时,您的Ajax请求被触发两次。要避免这种情况,请执行以下操作

首先,修复
.live()

然后使用以下命令终止
pagebeforeshow
以前的任何绑定

$(document).off("pagebeforeshow", "#showprofile").on("pagebeforeshow", "#showprofile", function () { });

.live()
不再使用,请改用上的
。您使用的是什么版本的jQ和jQM?我使用的是jQM版本1.2.0,我尝试了。在上,但没有按预期工作。是
$('.showuserinfo')。live('tap',function(event){
,在一个被多次调用的地方,因为这听起来像是正在发生的事情。我无法结束ajax调用。函数必须恰好位于该地方。
$(document).on('tap','.showuserinfo',function(event){
尝试使用此功能以及
$(document).off(“pagebeforeshow”,“showprofile”).on(“pagebeforeshow”,“showprofile”,function(){})
$(document).off("pagebeforeshow", "#showprofile").on("pagebeforeshow", "#showprofile", function () { });