函数调用ajax中的错误

时间:2010-10-11 14:31:45

标签: jquery

function manageVoting() {
    var parameter;
    var myVoting;
    var divVoting;
    var divVotes;
    var value = -1;
    var parameterData;
    $('div.votemaincontainer').each(function() {
        parameter = $(this).find('#[id$= hfUrl]').val();
        myVoting = parseInt($(this).find('#[id$=hfMyVote]').val());
        divVoting = $(this).find('[id$=divVoting]');
        divVotes = $(this).find('[id$=divVotes]');


        $('img.voteupImage').live('click', function() {
            if (myVoting == 1) {
                alert(" you have already voted");
            }
            else {
                value = 1;
            }
        });
        $('img.votedownImage').live('click', function() {
            if (myVoting == 0) {
                alert(" you have already voted");
            }
            else {
                value = 0;
            }
        });


        if (value == 0 || value == 1) 
        {
            parameterData = parameter + value + "'}";

            $.ajax({
                type: 'POST',
                url: 'UserControls/Vote/VoteAction.aspx/Voting',
                data: parameterData,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    var result = eval(data.d);

                    if (result) {
                        if (result.length > 1) {
                            alert("i am inside result here result length >1");
                            if (result[1] == 1 && result[2] == 1) 
                            {
                                $('img.voteupImage').attr('src', 'UserControls/Vote/Images/aftervote_arrow_up.png');
                                $('img.votedownImage').attr('src', 'UserControls/Vote/Images/arrow_down.png');
                                $('div.divVotes').html(result[0]);
                            }
                            else 
                            {
                                alert('U can not vote more than 1');
                            }
                            $('#[id$=hfMyVote]').html(result[1]);
                            if (result[1] == 0 && result[2] == 1) 
                            {
                                $('img.voteupImage').attr('src', 'UserControls/Vote/Images/Arrow_Up.png');
                                $('img.votedownImage').attr('src', 'UserControls/Vote/Images/aftervote_down.png');
                                $('div.divVotes').html(result[0]);  
                            }
                            else 
                            {
                                alert('U can not vote down more than 1');
                            }
                        }
                        else {
                            $('div.divVotes').html(result[0] - 1);
                            alertDialog("Rating any knowledge item is only available for Registered User.<br>Do you want to <a class='signUpPopUp' href='signup.aspx'> signup</a> Now?");
                        }

                    }
                },
                error: function() {
                    alert("i am inside error");

                }
            });
        }
    });
}



$(function() {
    manageVoting();
});

我已经编写了上面的代码但当我点击图片时,$ .ajax没有执行代码中的问题。

1 个答案:

答案 0 :(得分:1)

click实时处理程序中,您只需设置value参数的值。之后你没有执行其余的代码。尝试这样的事情(未经测试):

function manageVoting() {
    var parameter;
    var myVoting;
    var divVoting;
    var divVotes;
    var value = -1;
    var parameterData;
    $('div.votemaincontainer').each(function() {
        parameter = $(this).find('#[id$= hfUrl]').val();
        myVoting = parseInt($(this).find('#[id$=hfMyVote]').val());
        divVoting = $(this).find('[id$=divVoting]');
        divVotes = $(this).find('[id$=divVotes]');

        function processVote(value) {
            if (value == 0 || value == 1) {
                parameterData = parameter + value + "'}";

                $.ajax({
                    type: 'POST',
                    url: 'UserControls/Vote/VoteAction.aspx/Voting',
                    data: parameterData,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(data) {
                        var result = eval(data.d);

                        if (result) {
                            if (result.length > 1) {
                                alert("i am inside result here result length >1");
                                if (result[1] == 1 && result[2] == 1) 
                                {
                                    $('img.voteupImage').attr('src', 'UserControls/Vote/Images/aftervote_arrow_up.png');
                                    $('img.votedownImage').attr('src', 'UserControls/Vote/Images/arrow_down.png');
                                    $('div.divVotes').html(result[0]);
                                }
                                else 
                                {
                                    alert('U can not vote more than 1');
                                }
                                $('#[id$=hfMyVote]').html(result[1]);
                                if (result[1] == 0 && result[2] == 1) 
                                {
                                    $('img.voteupImage').attr('src', 'UserControls/Vote/Images/Arrow_Up.png');
                                    $('img.votedownImage').attr('src', 'UserControls/Vote/Images/aftervote_down.png');
                                    $('div.divVotes').html(result[0]);  
                                }
                                else 
                                {
                                    alert('U can not vote down more than 1');
                                }
                            }
                            else {
                                $('div.divVotes').html(result[0] - 1);
                                alertDialog("Rating any knowledge item is only available for Registered User.<br>Do you want to <a class='signUpPopUp' href='signup.aspx'> signup</a> Now?");
                            }

                        }
                    },
                    error: function() {
                        alert("i am inside error");

                    }
                });
            }
        }

        $('img.voteupImage').live('click', function() {
            if (myVoting == 1) {
                alert(" you have already voted");
            }
            else {
                value = 1;
                processVote(value);
            }
        });
        $('img.votedownImage').live('click', function() {
            if (myVoting == 0) {
                alert(" you have already voted");
            }
            else {
                value = 0;
                processVote(value);
            }
        });
    });
}

$(function() {
    manageVoting();
});

此外,当您将JSON数据类型与$.ajax一起使用时,您不需要eval返回的数据。 jQuery已经为你评估了JSON,并返回一个本机JavaScript对象。