weixin_33690963 2015-04-05 18:01 采纳率: 0%
浏览 138

Ajax未发送请求

I have been struggling on this simple problem for pretty long already, yes I tried to google for answer though with no luck, the problem is that my ajax request doesn't even send a thing, so I guess it has something to do with the syntax though it looks perfectly fine to me:

$(window).on('load',function()
{
    $("#unfriend_user > p").click(function()
    {
        $.ajax(
        {
            url: "php_includes/remove_friend.php", 
            type: "POST",
            data: 
            {
                user_1 : <?php echo $logged_id ?> ,
                user_2 : <?php echo $page_user_id ?>,
                user_username : <?php echo $logged_username ?>,
                user_password : <?php echo $logged_password ?>
            },
            success: function(data)
            {
                alert(data);
            }
        });
    });
});

All php echoed variables have values.

The problem came up after I added these two lines:

user_username : <?php echo $logged_username ?>,
user_password : <?php echo $logged_password ?>

If I remove these two lines and "," everything works just fine.

  • 写回答

2条回答 默认 最新

  • weixin_33724046 2015-04-05 18:05
    关注

    If you look at your rendered HTML ("View Source" in the browser), you'll see that your javascript looks something like:

    data: 
        {
            user_1 : 1 ,
            user_2 : 2,
            user_username : someusername,
            user_password : somepassword
       },
    

    Since those are strings (I'm assuming), you need to quote them:

    data: 
        {
            user_1 : <?php echo $logged_id ?> ,
            user_2 : <?php echo $page_user_id ?>,
            user_username : "<?php echo $logged_username ?>",
            user_password : "<?php echo $logged_password ?>"
        },
    
    评论

报告相同问题?