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",
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统
  • ¥15 快手联盟怎么快速的跑出建立模型