dprnr5559 2015-11-13 09:05
浏览 63
已采纳

为什么这会返回0?

I'm trying to make a custom captcha generator. In short, the server returns a URL of an image through AJAX and keeps in session storage an association with the image.

PHP:

add_action( 'wp_ajax_set_animal_captcha', 'set_animal_captcha' );
$capdir = get_template_directory_uri() . '/assets/captcha/';
$capmap = array ( 'cat'  => $capdir . 'Captcha_Cat.png',
                  'dog'  => $capdir . 'Captcha_Dog.png',
                  'fish' => $capdir . 'Captcha_Fish.png' );
function set_animal_captcha ( )
{
    // returns image url of random animal and stores in session storage
    // a reference to that animal
    $randAnimal = array_rand($capmap,1);
    $_SESSION['randAnimal'] = $randAnimal;
    die(json_encode($capmap[$randAnimal]));
}

JS:

function capHandler ( imgid )
{
    // imgid: id of the image 

    this.imgid = imgid;
    this.formData = new FormData();
    this.formData.append('action', 'set_animal_captcha');
    this.set = function ( )
    {
        $.ajax({
            url: ajaxurl,
            type: 'POST',
            async: false,
            success: function ( animalUrl ) { alert(animalUrl); },
            error: function ( ) { alert("Error in getting captcha image") },
            cache: false,
            contentType: false,
            processData: false
        });
    }
}

My problem is that it's alerting

0

and I can't figure out why. Because I know the success function is being called, there must be something wrong with the PHP.

展开全部

  • 写回答

2条回答 默认 最新

  • dosin84644 2015-11-13 09:33
    关注

    Why are you setting $capmap outside of set_animal_captcha()? If you really must keep it outside set_animal_captcha, then set $capmap as a global inside your function. Like this:

    function set_animal_captcha ( )
    {
        global $capmap;
        // returns image url of random animal and stores in session storage
        // a reference to that animal
        $randAnimal = array_rand($capmap,1);
        $_SESSION['randAnimal'] = $randAnimal;
        die(json_encode($capmap[$randAnimal]));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥20 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
  • ¥15 软件冲突问题,软件残留问题
  • ¥30 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥50 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥15 alpha101因子里哪些适合crypto?
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部