簡體   English   中英

使用JavaScript / jQuery調用do_action

[英]Call do_action using JavaScript/jQuery

我有一個AJAX函數,通過Facebook成功登錄后,將信息發送到userpro_ajax_url

我正在嘗試使用成功塊來運行do_action函數

<?php 
ob_start();  
do_action('userpro_social_login', <email needs to go here>);
ob_clean();
?>

現在如果我手動傳遞電子郵件地址它工作正常,但我能動態獲取電子郵件的唯一方法是通過JavaScript中的當前響應。

完整的功能是:

FB.api('/me?fields=name,email,first_name,last_name,gender', function(response) {
    jQuery.ajax({
        url: userpro_ajax_url,
        data: "action=userpro_fbconnect&id="+response.id+"&username="+response.username+"&first_name="+response.first_name+"&last_name="+response.last_name+"&gender="+response.gender+"&email="+response.email+"&name="+response.name+"&link="+response.link+"&profilepicture="+encodeURIComponent(profilepicture)+"&redirect="+redirect,
        dataType: 'JSON',
        type: 'POST',
        success:function(data){
            userpro_end_load( form );
            <?php 
            ob_start();  
            do_action('userpro_social_login', );
            ob_clean();
            ?>
            /* custom message */
            if (data.custom_message){
                form.parents('.userpro').find('.userpro-body').prepend( data.custom_message );
            }
            /* redirect after form */
            if (data.redirect_uri){
                if (data.redirect_uri =='refresh') {
                    //document.location.href=jQuery(location).attr('href');
                } else {
                    //document.location.href=data.redirect_uri;
                }
            }
        },
        error: function(){
            alert('Something wrong happened.');
        }
    });
});

我嘗試使用以下命令運行php動作:

jQuery(document).trigger('userpro_social_login', response.email);

這些都不起作用......我做錯了什么?

修改你的php函數userpro_fbconnect觸發那里的代碼,或者在完成后再創建另一個ajax請求

您不能從JS執行PHP代碼,反之亦然。 但以下內容可能對您有所幫助:

<?php
   // Some php code goes here...
?>
<script>
   // JS code...
   ob_start();                                                                    
   do_action('userpro_social_login', <?php <email needs to go here> ?>);
   ob_clean();
</script>

如果你想從你的JS執行一個方法,並使用PHP定義該方法,並將它傳到全局范圍,那么只需從JS my_global_method()調用它

我希望這有助於解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM