php - 为什么我的帖子 ajax 不起作用?

标签 php jquery ajax facebook facebook-graph-api

我所做的是facebook请求对话框并从javascript获取用户ID,然后发布到php代码以将userid插入我的数据库中,在我的facebook请求对话框部分我可以获得用户ID和请求ID,只有ajax发布部分不工作。

我的 JavaScript 编码:

function newInvite(){
    FB.ui({ 
    method : 'apprequests',
    title: 'X-MATCH',
     message: 'Come join US now, having fun here',                      
},                      
    function(response){
        var receiverIDs;
        if (response.request)  {
        var receiverIDs = response.to;  // GET USER ID
        alert(receiverIDs);                           
        //I stuck from here
        $.ajax({
            type: "POST",                                       
            url:"<?=$fbconfig['baseUrl']?>/ajax2.php",                                            
            data : {receiverIDs :receiverIDs},
            success: function(msg){
                alert(msg);
            },
            error: function(msg){
            alert(msg);
            }
        });
        }
    }               
);
}

我的php代码(文件名是ajax2.php):

<?php       
include 'config/config.php';
include_once "index.php";  
$Currentdatetime = date("Y-m-d h:i:s" ,strtotime("now"));                       
$senderID = $_POST['receiverIDs'];
$explode = explode(',', $senderID);
for ($i=0; $i<count($explode);$i++){
   mysql_query("INSERT INTO user_invite VALUES('$userid','$explode[$i]','0','50','$Currentdatetime','0')");
}
?>

有什么解决办法吗?谢谢

最佳答案

您需要将 jquery 添加到 header 中:

<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js'></script>

使用该标签,并阅读 Downloading jQuery文章了解 CDN。 Martin 是对的,这是静态内容(例如 jquery)的最佳方式,但重要的是要了解您在做什么,而不仅仅是去做。

尝试将 URL 更改为:

$.ajax({
        type: "POST",                                       
        url: "<?=$fbconfig['baseUrl']?>/ajax2.php",                                             
        data : { receiverIDs :receiverIDs },
        success: function(msg){
            alert(msg);
        },
        error: function(msg){
        alert(msg);
        }
    });

您不需要在其周围添加 2 组引号。

关于php - 为什么我的帖子 ajax 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8265380/

相关文章:

php - pcntl_signal 函数未被命中,使用套接字时 CTRL+C 不起作用

jquery - 为 jquery fancybox 灯箱添加滑动效果

php - 小部件类中的 AJAX 函数

php - 在 html 中回显 html 值

php - 无法将 Illuminate\Routing\Controller 用作 Controller ,因为该名称已被使用

php - 用 sed 替换模式中的字符

javascript - 更改脚本 : Loop through divs every 10 seconds PLUS select DIV

jquery - Ajax返回,json数据

javascript - Ember 使用 ajax 动态加载模板

javascript - 使用 Isotope 和 imagesLoaded 来处理 Ajax 加载的内容