You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress Ajax无法获取二进制数据仅返回<?php的问题解决

WordPress Ajax 获取图片二进制数据异常问题及解决办法

在非WordPress环境中,通过Ajax调用可正常获取图片等二进制数据,但在WordPress中调用admin-ajax.php时,仅返回<?php,无法得到预期结果。以下是测试代码及最终解决方案:

非WordPress环境可用代码

PHP代码

get_image();
function get_image(){   
    $file='Large-Sample-Image-download-for-Testing.jpg';
    // 设置为可下载
    header("content-disposition: attachment; filename=test.jpg");
    header('cache-control: must-revalidate');
    header("expires: 0");            
    // 返回文件内容
    readfile($file);
}

HTML及JS代码

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js" ></script>
<script>
jQuery(document).ready(function($) { 
    $("#ajax_get_image_button").click(function() {        
        var xhr = new XMLHttpRequest();
        xhr.open('POST','get_image.php', true);
        xhr.responseType = "blob"; 
        xhr.onload = function() {
            //console.log(this.response);
            var img = document.createElement('img');
            img.src=URL.createObjectURL(this.response); 
            document.body.append(img);
        };
        xhr.send();
    });
});
</script>
</head>
<body>
<input type="button" id="ajax_get_image_button" name="ajax_get_image_button" value="Ajax调用获取图片" />
</body>
</html>

WordPress环境问题代码

HTML部分

<div><input type="button" name="ajax_get_image_button" id="ajax_get_image_button" value="Ajax调用获取图片"  class="button-primary"/>

JS部分

$("#ajax_get_image_button").click(function() {        
    var data = {                    
            _ajax_nonce: my_js_obj.nonce, 
            action: "func_get_image",
        };
    url=my_js_obj.ajax_url;
    // 发起XHR请求
    var xhr = new XMLHttpRequest();
    xhr.open('POST', url, true);
    xhr.responseType = "blob";
    xhr.onload = function() {
        var img = document.createElement('img');
        img.src=URL.createObjectURL(this.response); 
        document.body.append(img);
    };

    xhr.send($.param( data ));
});

PHP部分

add_action('admin_enqueue_scripts','my_javascript');
function my_javascript($hook) {
    global $current_screen;
    if( $hook !== $current_screen->base ) return; // 仅在指定页面加载脚本
        wp_enqueue_script( 
        'my_js', 
        plugins_url( 'js/get_image.js', __FILE__ ),
        array('jquery'), 
        '1.0.0',
    );
    wp_localize_script(
        'my_js',
        'my_js_obj',
        array(
            'ajax_url' => admin_url('admin-ajax.php' ),
            'nonce'    => wp_create_nonce( 'my_nonce' ),
        )
    );

}
...
add_action( 'wp_ajax_my_get_image', 'func_get_image' );
function func_get_image(){
    check_ajax_referer( 'my_nonce');
    get_image();
    wp_die();
}

原本认为WordPress仅通过钩子执行脚本,逻辑应和常规PHP一致,但实际结果不符。最终通过修正文件路径解决问题:

readfile(plugin_dir_path( __FILE__ ) . '/'. $file);

内容的提问来源于stack exchange,提问作者pang-db-builder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 00:15:28