0
function follow_or_unfollow(following,follower,action)
{
var dataString = "following=" + following + "&follower=" + follower;
$.ajax({  
    type: "POST",  
    url: "network.callaction.php",  
    data: dataString,
    cache: false,
    beforeSend: function() 
    {
        if ( action == "following" )
        {
            $("#following").hide();
            $("#loading").html();
        }
        else if ( action == "follow" )
        {
            $("#follow").hide();
            $("#loading").html();
        }
        else { }
    },  
    success: function(response)
    {
        if ( action == "following" )
        {
            $("#loading").html('');
            $("#follow").show();

        }
        else if ( action == "follow" )
        {
            $("#loading").html('');
            $("#following").show();
        }
        else { }
    }
}); 
}

在这种情况下,如果 action 是 FOLLOW,那么应该调用不同的 url,如果 action 是 UNFOLLOW,那么应该调用不同的 url。

那么我们可以在 URL 部分使用 if 条件吗?

4

3 回答 3

0

很简单:

function follow_or_unfollow(following,follower,action)
{

    var actionUrl = 'follow.callaction.php';
    if(action == 'unfollow'){
        actionUrl = 'unfollow.callaction.php';
    }

    var dataString = "following=" + following + "&follower=" + follower;

    $.ajax({  
        type: "POST",  
        url: actionUrl,  
        data: dataString,
        cache: false,
        beforeSend: function() 
        {
            if ( action == "following" )
            {
                $("#following").hide();
                $("#loading").html();
            }
            else if ( action == "follow" )
            {
                $("#follow").hide();
                $("#loading").html();
            }
            else { }
        },  
        success: function(response)
        {
            if ( action == "following" )
            {
                $("#loading").html('');
                $("#follow").show();

            }
            else if ( action == "follow" )
            {
                $("#loading").html('');
                $("#following").show();
            }
            else { }
        }
    }); 
}

你也可以这样做:

var actionUrl = action + '.callaction.php';
于 2013-02-27T08:32:21.853 回答
0

最好将条件从 AJAX 中取出并像这样分配变量。

if(condition) {
     var pageURL = "example.php";
} else {
     var pageURL = "example1.php";
}

$.ajax({  
    type: "POST",  
    url: pageURL,
于 2013-02-27T08:32:43.583 回答
0

外部ajax调用:

if(yourconditin){
   url = "url1.php";
}
else{
   url = "url2.php";
}

在 ajax 中给它这样的:

url: url,
于 2013-02-27T08:31:45.903 回答