Cannot modify the status message when the status code is 200

时间:2015-07-28 15:46:13

标签: jquery wordpress-plugin http-headers

I have a wordpress plugin with some functionality that when I return a particular status message the plugin will print a specific error message to my plugin admin page. However when I get the status code 200 it only sends the message ok. I would like to control what message is sent when the status code is 200. Here is my php code:

public function email_service_page() {
    if (isset($_POST['term'])) {
        update_option('email-content', $_POST['term']);
    }

    $post_url = $_POST['term'];

    // Variable to store the post ID
    $store_post_id = url_to_postid($post_url);

    $to_return = array();
    $post_date = get_the_date( "U", $store_post_id );
    $tags      = get_the_tags( $store_post_id );
    $main_tag  = "";
    if ( is_array( $tags ) ) {
        foreach ( $tags as $tag ) {
            if ( $tag->slug == 'featured' ) {
                $main_tag = 'Featured';
            } else if ( $tag->slug == 'highlight' ) {
                $main_tag = 'Highlight';
            }
        }
    }

    $to_return['tag']           = $main_tag;
    $to_return['ID']            = $store_post_id;
    $to_return['date']          = date( "M j, Y", $post_date );
    $to_return['hour']          = date( "H:i", $post_date );
    $to_return['post_title']    = get_the_title($store_post_id);
    $to_return['post_excerpt']  = $this->get_excerpt_by_id($store_post_id);

    $response = json_encode($to_return);

    if( $store_post_id == 0 ){
        header( 'HTTP/1.1 200 Not a post' );
        die();
    }else{
        header( 'HTTP/1.1 200 Post found' );
        echo $response;
    }


    exit;

}

here is my javascript:

jQuery(document).ready(function () {

    jQuery("preview-email-form").submit(function() {
        jQuery('#preview-email-submit').attr('disabled', true);

        data = {
            action: 'email_service_page',
            bbna_nonce: bbna_vars.bbna_nonce,
            'term': jQuery("#email-content").val()
        };

        jQuery.ajax({
            type: "POST",
            url: ajaxurl,
            data: data,
            dataType: "json",
            success: function(response, textStatus, jqXHR){
                postSearchCallback(response, textStatus, jqXHR);
            }
        });

        return false;
    });
  
  });

function postSearchCallback(data, textStatus, jqXHR){
    console.log(jqXHR);
    if (jqXHR.statusText == "Post found"){
        appendNewRowToList(data);
    } else if (jqXHR.statusText == "Not authorized") {
        jQuery('#add-post-by-url-message').html('<p style="font-family:monospace; color:red;">Access is denied. Try reloading the page.</p>');
    } else if (jqXHR.statusText == "Not a post" && jqXHR.statusText == "Post found") {
        jQuery('#add-post-by-url-message').html('<p style="font-family:monospace; color:red;">URL provided is not a story.</p>');
    } else {
        jQuery('#add-post-by-url-message').html('<p style="font-family:monospace; color:red;">Post not found.</p>');
    }
    jQuery('#bbna-preview-email-submit').attr('disabled', false);
    jQuery("#bbna-email-content").val('');
}

Despite the fact that I am writing a string in the head function to be the following header( 'HTTP/1.1 200 Post found' ); or header( 'HTTP/1.1 200 Not a Post' ); I keep getting a status text of ok.

0 个答案:

没有答案