ajax - Using wp_send_json_success instead of $result['message'], die, etc

My usual ajax function looks like this:function get_modal_playlist_classplan( $odv_video_id ) {check_ajax_referer($_REQ

My usual ajax function looks like this:

function get_modal_playlist_classplan( $odv_video_id ) {
    check_ajax_referer($_REQUEST['nonce'], "my_cool_nonce", false);

    $result['type'] = "success";

    $result['message'] = "Booooo";

    if ( $some_test === true ) {
        $result['message'] = "Yay";
    }

    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        $result = json_encode($result);
        echo $result;
    } else {
        header("Location: " . $_SERVER["HTTP_REFERER"]);
    }

    die();
}

How do I replace my approach with the use of wp_send_json_success (and wp_send_json_error)?

I tried replacing $result['message'] = "yay" with:

$return = array(
    'message' => 'yay',
    'some_other_data' => $_REQUEST['hello'] . ' world'
);

wp_send_json_success( $return );

But the ajax request fails.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741986808a4377221.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信