$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Admin-ajax.php 400 error|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Admin-ajax.php 400 error

matteradmin9PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far