AJAX请求的“错误请求” 400服务器响应

时间:2019-03-01 11:17:36

标签: javascript php jquery ajax

所有常见的可疑对象都被尝试无效,包括设置:ContentType:和dataType :。如果未指定,则jQuery均具有默认值。尝试使用JSON.stringify(data)序列化json-上面的所有组合都不会更改服务器响应。无法看到我在这里缺少的原因。

这是有问题的javascript:

jQuery(document).ready(function() {
    jQuery('.delete_listing').on('click', (function(e) {
        var post_type = jQuery(this).data( 'type' );  // Get post type via the 'data-type' attribute of the button.

        e.preventDefault();

        if (confirm('You want to delete this listing?')) {  

            jQuery.ajax({
                url: fpf_ajax_delete_listing.ajax_url, // 'fpf_ajax_delete_listing' is from wp_localize_script() call
                type: 'post',             
                data: {
                    action: 'fpf_ajax_delete_listing_process', // part of add_action() call
                    nonce: fpf_ajax_delete_listing.fpf_delete_listing_nonce, // from wp_localize_script() call.
                    post_type : post_type
                },

                success : function( response ) {
                    console.log(response);
                },

                error : function( response) {
                    alert('Error retrieving the information: ' + response.status + ' ' + response.statusText);
                    console.log(response);
                }

            });

            // clear edit page, msg delete successful, redirect to seller account dashboard
        jQuery(function() {
            jQuery(".form-group").remove();
            jQuery("p").remove();
            jQuery("#remove-listing").remove();
            jQuery("hr").replaceWith('<h4 style="color:green;"><em>Listing successfully deleted.</em></h4>');

        // return to seller dashboard

        }); // clear & redirect

    } else {
        // Do nothing!
    }

    }));

});

这是插件中的PHP:

function fpf_ajax_delete_listing_shortcode() {
    return '<button class="delete_listing" data-type="post" type="button" style="background:red; border-radius:5px; border-width:1px; color:white;">Delete Listing</button>';
}
add_shortcode('ajax_delete_listing', 'fpf_ajax_delete_listing_shortcode');

add_action('wp_enqueue_scripts', 'fpf_enqueue_scripts');
function fpf_enqueue_scripts() {
    wp_enqueue_script( 'fpf-delete-listing', plugin_dir_url( __FILE__ ). 'ajax-delete-listing-code.js', array('jquery') );
    wp_localize_script( 'fpf-delete-listing', 'fpf_ajax_delete_listing', array(
                        'ajax_url' => admin_url( 'admin-ajax.php' ),
                        'fpf_delete_listing_nonce' => wp_create_nonce('fpf-delete-listing-nonce')
    ));
 }

add_action( 'wp_ajax_fpf_ajax_delete_listing', 'fpf_ajax_delete_listing', 20 );  // logged in users only.
function fpf_ajax_delete_listing() {
    check_ajax_referer( 'fpf-delete-listing-nonce', 'nonce' );  // dies if nonce is not correct.
    $post_id = get_the_ID();
    update_post_meta($post_id, 'wpcf-listing-status', 'inactive');
}
return;

0 个答案:

没有答案