如何从前台发送AJAX调用而不在主题中使用wp_LOCALIZE_脚本

时间:2018-01-19 作者:Rohit Yadav

我的情况是,我必须在不使用任何插件的情况下从前端发送数据,因为代码正在使用Ajax加载到同一页面上,所以我不能使用任何插件,因为插件文件将在刷新文件时加载,但页面未加载。

我正在使用post snippet plugin 将PHP和JS发送到页面上正在加载的页面。PHP Code is:-

if ( is_user_logged_in() ) {
global $current_user;
$unit_id= get_the_ID();
$userid = $current_user->ID;
global $wpdb;
$results = $wpdb->get_row( "SELECT * FROM wp_prepze_video_limits WHERE user_id =$userid && unit_id = $unit_id");
$counter = $results->counter; 
if($counter!=0)
    {
        echo $counter;
    }
else
{
    $myObj->counter = $counter;
    $myObj->unit = $unit_id;
    $myObj->user = $userid;
    $myJSON = json_encode($myObj);
    echo $myJSON;
}
}

JS CODE is:-

$(\'.clickable\').on(\'click\', function(){
        var counter = [uname][/uname]; //This is post snippet Name that outputs Above PHP Code in here
        var user_counter;
        if(counter.counter==null)
        {
            user_counter = 0;   
        }
        //var user_counter= counter.counter;
        var unit_id = counter.unit;
        var user = counter.user;
        var ajax= counter.ajax_url ;
        jQuery.ajax({

            url : AjaxUrl,// I\'m Not Sure Where do I Send the Request
            type : \'post\',
            data : {
                action : \'post_love_add_love\',
                unit_id : unit_id,
                user_counter : user_counter,
                user : user,

            },
            success : function( response ) {
                //jQuery(\'#love-count\').html( response );
                console.log(response);
            }
        });
    });
I am not sure about where do, I send this request.

1 个回复
最合适的回答,由SO网友:Rohit Yadav 整理而成

jQuery.ajax({
                    url : postlove.ajax_url,
                    type : \'post\',
                    data : {
                        action : \'post_love_add_love\',
                        unit_id : unit,
                        user_counter : new_counter,
                        user : user,

                    },
                    success : function( response ) {
                        //jQuery(\'#love-count\').html( response );
                        console.log(response);
                    }
                });
这对我有用。谢谢

结束