Wordpress Ajax Call在第一种情况下有效,在第二种情况下返回400 Bad Request

时间:2019-07-16 09:42:20

标签: jquery ajax wordpress http-status-code-400 bad-request

我正在为某些WordPress功能使用ajax函数,但在第一种情况下调用具有相同参数的相同函数时遇到一些问题,在第二种情况下返回400

这是我的第一个工作电话

首先,我创建一个上传的文件:

//My JS call
var dataURL = canvas.toDataURL('image/jpeg');       
    var data = { 'action'    : 'add_image_action', // The name of the WP action
                 'imageData' : dataURL
            };
    $.post(snap.ajaxurl, data, function(response) {         
        console.log(response);
        console.log(response.thumbnail);
        snapImage = response.thumbnail;
        return snapImage;
    });

//php callback
function add_image_callback() { 
if ( isset ( $_POST['imageData'] )) {       
    ob_start(); 
    $uniqueid = uniqid();
    define('UPLOAD_DIR', get_uploads_directory_path() . 'preloads/');
    $img      = $_POST['imageData'];
    $img      = str_replace('data:image/jpeg;base64,', '', $img);
    $img      = str_replace(' ', '+', $img);
    $data     = base64_decode($img);
    $thumbnail   = $uniqueid. '.jpeg';
    $file     = UPLOAD_DIR . $thumbnail;
    $success  = @file_put_contents($file, $data);
    $response = $success ? $uniqueid : "Unable to load file";

    ob_end_clean(); 

    wp_send_json( array( 'add_image_callback()' => 'OK',
                         'thumbnail'            => $thumbnail) );
} else {
    wp_send_json( array( 'add_video_callback()' => 'NOK') );
}
wp_die();
}
add_action( 'wp_ajax_add_image_action', 'add_image_callback' );
add_action( 'wp_ajax_nopriv_add_image_action', 'add_image_callback' );

然后我从前端适用于第一种情况添加我的自定义帖子类型:

//My second call JS
sendButton.click(function (e) {
    e.preventDefault();
    title    = sTitle.val();
    content  = sContent.val();  
    data = {    'action'       : 'add_advert_action', // The name of the WP action              'title'    : title,
                'content'  : content,
                'image'    : image, 
           };   
    console.log(data);
    $.post(snap.ajaxurl, data, function(response) {         
        console.log(response);
        console.log(response.id);
        var postID = response.id;
        $(location).attr('href','/?page_id='+ postID +'');
    });
});

//my PHP callback
function add_advert_callback() {
ob_start();
if(isset($_POST['newSnap']) == '1') {
    $post_title      = $_POST['title'];
    $post_content    = $_POST['content'];
    $post_image      = $_POST['image'];
    $imageSelector   = 'image';                 
    $image           = '/wp-content/uploads/preloads/' . $post_image;       

    $new_post = array(
          'ID' => '',
          'post_author' => $user->ID, 
          'post_content' => $post_content, 
          'post_title' => $post_title,
          'post_status' => 'publish',
          'post_type'=>'annonces', 
        );

    $post_id = wp_insert_post($new_post);

    update_field($imageSelector, $image, $post_id);

    generate_Featured_Image( $image, $post_id );
    ob_end_clean(); 
    wp_send_json( array( 'add_advert_callback' => '1',
                         'id' => $post_id ) );
} else {
    wp_send_json( array( 'add_advert_callback' => '0',
                         'id' => $post_id ) );
}
wp_die();
}
add_action( 'wp_ajax_add_advert_action', 'add_advert_callback');
add_action( 'wp_ajax_nopriv_add_advert_action', 'add_advert_callback');

这是第二个通话不起作用

首先创建一个文件和一个视频,正常运行

fd = new FormData();
fd.append('file', blob, 'blob.mp4');
fd.append('action', 'add_video_action');                    
fd.append('screenshot', screenshot);
if (blob !== undefined || blob !== null && isTapHold === false) {               
$.ajaxSetup({
    contentType: false,
    processData: false
});
$.post(snap.ajaxurl, fd, function(response) {
    console.log(response);
    image  = response.thumbnail;
    return image;
}); 
}

function add_video_callback() { 
ob_start();     
if ( isset ($_FILES['file']) && $_POST['screenshot'] )  {
    define('UPLOAD_DIR', get_uploads_directory_path() . 'preloads/');
    $uniqueid    = uniqid();

    $screenshoot = $_POST['screenshot'];
    $screenshoot = str_replace('data:image/jpeg;base64,', '', $screenshoot);
    $screenshoot = str_replace(' ', '+', $screenshoot);
    $data        = base64_decode($screenshoot);     
    $thumbnail   = $uniqueid . '.jpeg';
    $videoname   = $uniqueid . '.mp4';
    $file        = UPLOAD_DIR . $videoname;
    $screenshot  = UPLOAD_DIR . $thumbnail;
    @file_put_contents($screenshot, $data);
    move_uploaded_file( $_FILES['file']['tmp_name'], $file );
    ob_end_clean(); 
    wp_send_json( array( 'add_video_callback()'  => 'OK',
                         'thumbnail'             => $thumbnail )); 
} else {
    wp_send_json( array( 'add_video_callback()' => 'NOK' ));
}
wp_die();
}
add_action( 'wp_ajax_add_video_action', 'add_video_callback' );
add_action( 'wp_ajax_nopriv_add_video_action', 'add_video_callback' );

第二个通话案例返回了400个错误请求

 //My second call JS
sendButton.click(function (e) {
    e.preventDefault();
    title    = sTitle.val();
    content  = sContent.val();  
    data = {    'action'       : 'add_advert_action', // The name of the WP action              'title'    : title,
                'content'  : content,
                'image'    : image, 
           };   
    console.log(data);
    $.post(snap.ajaxurl, data, function(response) {         
        console.log(response);
        console.log(response.id);
        var postID = response.id;
        $(location).attr('href','/?page_id='+ postID +'');
    });
});

我使用相同的表单,创建了图像并且触发事件相同。

在此方面非常需要帮助

0 个答案:

没有答案