【问题标题】:wordpress ajax Call to upload filewordpress ajax 调用上传文件
【发布时间】:2023-04-03 01:00:01
【问题描述】:

我正在尝试在 wordpress 中使用 ajax 上传文件。

我正在尝试以下形式:

 <form class="form-horizontal" id="file_form">

<?php  wp_nonce_field("ajax_file_nonce","security");  
 wp_localize_script( 'custom-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
 ?>

<input type="hidden" name="action" value="my_file_upload">
<input id="resume" name="resume" class="input-file form-control" type="file">

这是我的功能:

add_action('wp_ajax_my_file_upload', 'my_file_upload');

add_action('wp_ajax_nopriv_my_file_upload', 'my_file_upload');

function handle_file_upload()
{

    $response['message'] = 'Done!';
     echo json_encode($response); die();
     check_ajax_referer('ajax_file_nonce', 'security');

    if(!(is_array($_POST) && is_array($_FILES) && defined('DOING_AJAX') && DOING_AJAX)){
        return;
    }

    if(!function_exists('wp_handle_upload')){
        require_once(ABSPATH . 'wp-admin/includes/file.php');
    }
    $upload_overrides = array('test_form' => false);

    $response = array();

    foreach($_FILES as $file){
        $file_info = wp_handle_upload($file, $upload_overrides);

        // do something with the file info...
        $response['message'] = 'Done!';
    }

    echo json_encode($response);
    die();
}

这是我的 jquery:

jQuery(document).on(\'click\', \'#send\', function(e){
//alert();
e.preventDefault();
var ajax_url = "'.admin_url('admin-ajax.php').'"
    var form_data = {};
$("#file_form").find(\'input\').each(function(){
    form_data[this.name] = $(this).val();
    //alert(this.name);
    });


jQuery.ajax({
        type: 'POST',
        url: MyAjax.ajaxurl, // 
        data: form_data,
        contentType: 'json',
        success: function(response){
            alert(response.message);
        }
    }); 


 });

但这表明未定义 MyAjax。当我尝试在同一个 jquery 中定义的“ajax_url”变量时,它返回“未定义”消息。

这段代码有什么问题?需要进行哪些修改。请帮帮我。

【问题讨论】:

  • 我试图找到所有的例子,也尝试了一切。他们建议。但不知道我错过了什么。
  • 我没有成功,但我找到了这个插件并为我的使用进行了定制。 wordpress.org/plugins/alchemist-ajax-upload 感谢开发者..

标签: jquery ajax wordpress


【解决方案1】:

这可能取决于您如何加载 jQuery 脚本以及如何使用 wp_localize_script 函数。

在你的 functions.php 文件中试试这个:

add_action( 'wp_enqueue_scripts', function(){
    wp_enqueue_script( 'custom-ajax-request', get_stylesheet_directory_uri().'/js/custom-ajax-request.js', array('jquery') );
    wp_localize_script( 'custom-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
} );

wp_enqueue_script 函数加载您的 jQuery 脚本(我将文件命名为 custom-ajax-request.js),wp_localize_scriptMyAjax 变量传递给脚本(确保使用相同的句柄,在此案例'custom-ajax-request')。

您还应该取消引用 jQuery 脚本中的字符串,并从模板文件的表单中删除 wp_localize_script 行。

【讨论】:

  • 错误已解决:“MyAjax 未定义”。但返回 0,而不是返回“完成!”。
【解决方案2】:

删除

contentType: 'json'

来自 jquery 并尝试一下。

我在以前的堆栈答案中读过某处

【讨论】:

  • 它可以工作,但是当我尝试打印 $_files 数组时,它显示为空。我该怎么办?
猜你喜欢
  • 2017-09-23
  • 2018-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多