Admin-ajax.php 400 error

admin2025-06-03  3

Hi i'm just begining with wordpress so any help would be appreciated. I trying to get this working but the response data is undefined because of the 400 error. I've tried everything. Here's my code.

function simokydesigns_translate_scripts() {

   wp_enqueue_script('customjquery', get_template_directory_uri() 
   .'/js/customjquery.js', array('jquery'));
   wp_localize_script( 'customjquery', 'ajaxurl', admin_url( 'admin- 
   ajax.php' ) );
}
add_action( 'wp_enqueue_scripts', 'simokydesigns_translate_scripts' );

function get_ajax_sidebar(){

  get_sidebar();
}

add_action('wp_ajax_get_ajax_sidebar', 'get_ajax_sidebar');
add_action('wp_ajax_nopriv_get_ajax_sidebar', 'get_ajax_sidebar');

//js/customjquery.js

(function($) {

  $.ajax({
        type: 'POST', // use $_POST method to submit data
        dataType: "html",
        url: ajaxurl,
        data: {
            'action': 'get_ajax_sidebar',   
        },
        success:function(data) {

            alert('Got this from the server: ' + data);
            //$( '#widget-top' ).html( data );
        },
        error: function (data) {
            console.log('error' + data);
        }

    });  



     /*$( '#widget-top').append( "<?php echo get_sidebar();?>" );*/


 })( jQuery );

Hi i'm just begining with wordpress so any help would be appreciated. I trying to get this working but the response data is undefined because of the 400 error. I've tried everything. Here's my code.

function simokydesigns_translate_scripts() {

   wp_enqueue_script('customjquery', get_template_directory_uri() 
   .'/js/customjquery.js', array('jquery'));
   wp_localize_script( 'customjquery', 'ajaxurl', admin_url( 'admin- 
   ajax.php' ) );
}
add_action( 'wp_enqueue_scripts', 'simokydesigns_translate_scripts' );

function get_ajax_sidebar(){

  get_sidebar();
}

add_action('wp_ajax_get_ajax_sidebar', 'get_ajax_sidebar');
add_action('wp_ajax_nopriv_get_ajax_sidebar', 'get_ajax_sidebar');

//js/customjquery.js

(function($) {

  $.ajax({
        type: 'POST', // use $_POST method to submit data
        dataType: "html",
        url: ajaxurl,
        data: {
            'action': 'get_ajax_sidebar',   
        },
        success:function(data) {

            alert('Got this from the server: ' + data);
            //$( '#widget-top' ).html( data );
        },
        error: function (data) {
            console.log('error' + data);
        }

    });  



     /*$( '#widget-top').append( "<?php echo get_sidebar();?>" );*/


 })( jQuery );
Share Improve this question edited Jul 14, 2018 at 12:41 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Jul 14, 2018 at 12:39 SimokySimoky 111 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You have two problems in PHP you need change this

is bad:

wp_localize_script( 'customjquery', 'ajaxurl', admin_url( 'admin- 
   ajax.php' ) );

change to:

wp_localize_script( 'customjquery', 'my_custom_vars', ['ajax_url' => admin_url('admin-ajax.php')] );

and your javascript need this changes

is bad:

$.ajax({
        type: 'POST', // use $_POST method to submit data
        dataType: "html",
        url: ajaxurl,

change to:

$.ajax({
        type: 'POST', // use $_POST method to submit data
        dataType: "html",
        url: my_custom_vars.ajax_url,

This line break here:

wp_localize_script( 'customjquery', 'ajaxurl', admin_url( 'admin- 
   ajax.php' ) );

Means that the AJAX URL isn't correct. Make sure there's no line break:

wp_localize_script( 'customjquery', 'ajaxurl', admin_url( 'admin-ajax.php' ) );

When I made this fix and tested your code worked just fine.

But just as an aside, a POST request is inappropriate for this use case. You're getting data (in this case HTML for a sidebar), not submitting data, so a GET request should be used.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748950353a315088.html

最新回复(0)