最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论