doujiao8491 2019-02-08 17:09
浏览 159

数据变量返回为[object Object]

I have a button with data :

data-boutique-id="<?php echo esc_attr(get_the_ID());

In my main.js i have 2 jQuery functions using those 2 variables :

$fav = $('.fav-btn'),
boutiqueId = $fav.data('boutique-id');

My on('click') function is working fine, it does return the data-boutique-id. Bu my ready() function is not working and the variable boutiqueId is returning [object Object] instead of the Id. There must be something i don't know.

var $fav = $('.fav-btn'),
  boutiqueId = $fav.data('boutique-id');

$fav.ready(function(theBoutique) {     
  $.ajax({
    type: 'GET',
    data: {
      action: 'check-fav',
      boutiqueId: theBoutique
    },
    url: '/wp-json/fav_ajax/v1/manage_fav/'
  }).done(function(_data) { 
    console.log(_data);
  });
});
function manage_fav_rest_route() 
{
  // Path to ajax 
  register_rest_route(
    'fav_ajax/v1',
    '/manage_fav/',
    array(
      'methods' => WP_REST_SERVER::READABLE,
      'callback' => 'handle_rest_call'
    )
  );
}

function handle_rest_call() 
{
  $the_boutique = $_GET['boutiqueId'];
  $the_action =  $_GET['action'];

  if ('check-fav' == $the_action) 
  {
    return $the_boutique;
  }
}
  • 写回答

1条回答 默认 最新

  • dryb38654 2019-02-08 21:26
    关注

    I had to create an event with the function inside.

    $fav.ready(function( event ) {
        console.log( "event ready!" );
    
      // Validate user is logged in
      if( isLoggedIn) {
        checkFav( boutiqueId );
      }
      });
    
      function checkFav( theBoutique ) {
        console.log( "function ready!" );
    
            $.ajax({
            type: 'GET',
            data: {
              action : 'check-fav',
              boutiqueId : theBoutique
            },
           url: '/wp-json/fav_ajax/v1/manage_fav/'
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?