duanji7881 2016-07-21 11:01
浏览 73
已采纳

(Wordpress / Ajax)ReferenceError:找不到变量:ajaxobject

I'm working with Wordpress + Ajax and even using the proper hooks I get the error "ReferenceError: Can't find variable: ajaxobject". Of course there is some problem with my ajaxurl but I don't understand where since it seems well done to me. Can you help me?

In my functions.php

add_action( 'wp_enqueue_scripts', 'add_frontend_ajax_javascript_file', 11, 2 );
function add_frontend_ajax_javascript_file()
{
   wp_localize_script( 'ajax-script', 'ajaxobject', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}

my jQuery/AJAX file

var itemtitle = $('#itemtitle').val();
    var itemdescription = $('#itemdescription').val();
    jQuery.ajax({
        method: 'post',
        url : ajaxobject.ajaxurl, //Why???
        dataType: "json",
        data: {
            'action':'update_portfolio_function',
            'pid' : id,
            'itemtitle' : itemtitle,
            'itemdescription' : itemdescription,
        },
        success:function(data) {
            // This outputs the result of the ajax request
            alert("Coooool");
        },
        error: function(errorThrown){
            console.log(errorThrown);
        }

    });

of course the update_portfolio_function looks like

add_action('wp_ajax_update_portfolio', 'update_portfolio_function' );
function update_portfolio_function(){
    $id = $_REQUEST['pid'];
    $title = $_REQUEST['itemtitle'];
    $description = $_REQUEST['itemdescription'];
    $attachment = array(
        'ID' => $id,
        'post_title' => $title,
        'post_content' => $description
    );
    // now update main post body
    wp_update_post( $attachment );
    die();
}

Should I use init or no_priv?

  • 写回答

1条回答 默认 最新

  • dream5694 2016-07-21 16:04
    关注

    You just need to use ajaxurl instead of ajaxobject.ajaxurl

    like below

    jQuery.ajax({
            method: 'post',
            url : ajaxurl,
            dataType: "json",
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上