Support » Developing with WordPress » admin-ajax.php 400 (Bad Request) error

  • hi, I want to deliver html value through JS to PHP handling, but I still got error, 400 (Bad Request) .

    This is HTML:

    </div>
                <form class="row g-3" id="my_form">
                  <div class="col-md-12">
                    <label for="url" class="form-label">Input URL</label>
                    <input type="text" class="form-control my_info" id="url" required>
            
                  <div class="col-12">
                    <button class="btn btn-primary submit" type="button">Submit form</button>
                  </div>
                </form>
       </div>

    This is JavaScript:

    function getBaseUrl() {
        var re = new RegExp(/^.*\//);
        var my_local_url = re.exec(window.location.href)[0];
    
        return my_local_url;
    }
    
    console.log(getBaseUrl())
    
    jQuery(document).ready(function($) {
        console.log('test');
        // We'll pass this variable to the PHP function example_ajax_request
        var fruit = 'Banana';
        $('.submit').click(function (){
            // This does the ajax request
            $.ajax({
                url: getBaseUrl() + 'admin-ajax.php',
                data: {
                    'action':'crawler_info',
                    'my_info' : document.getElementById('url').value
                },
                success:function(data) {
                    // This outputs the result of the ajax request
                    console.log(data);
                },
                error: function(errorThrown){
                    console.log(errorThrown);
                }
            }); 
        })
    });

    This is PHP:

    function get_info() {
    
        // The $_REQUEST contains all the data sent via ajax 
        if ( isset($_REQUEST) ) {
    
            $infos = $_REQUEST['my_info'];
    
            // Now we'll return it to the javascript function
            // Anything outputted will be returned in the response
            echo $infos;
    
            // If you're debugging, it might be useful to see what was sent in the $_REQUEST
            print_r($_REQUEST);
    
        }
    
        // Always die in functions echoing ajax content or it will display 0 or another word
       die();
    }
    
    add_action( 'wp_ajax_crawler_info', 'get_info' );
    add_action( 'wp_ajax_nopriv_crawler_info', 'get_info' );

    Does anybody know how to solve my problem,Thanks

Viewing 3 replies - 16 through 18 (of 18 total)
  • Moderator bcworkz

    (@bcworkz)

    Oh, you mean how to display ajax return values on the page instead of simply console logging? It depends on what sort of data is returned. If it’s a JSON string data will need decoding before it can be used. But in the example’s case it’s a simple string. You’d get the DOM element in which you want the string to appear and apply .html() or .after() to it. Let’s say the element has the ID attribute “ajax-info”. You might do something like:

    .done(
    	function( data ){
    		$( "#ajax-info" ).html( data );				
    	}
    );
    Thread Starter nobody123

    (@nobody123)

    @bcworkz No, I mean display value using PHP, not JS.
    First, our discuss base on wordpress plugin, so we can display value on plugin page using php’s echo.
    Second, the process is the following
    html value –1–> JS get this value –2–> JS delivers this value using ajax–3–> php gets this value using wp_ajax_ –4–> displaying this value using php echo (This means php gets this value truly, and then I can handle this value or doing another testing)

    Another word, I don’t know how to modify your code to the 4th step.

    Moderator bcworkz

    (@bcworkz)

    I’m not sure what you’re really asking then. I feel like I’ve already answered how you’d see PHP data. It seems like you might be missing some fundamental understanding of what’s going on. I’ll try to explain the situation, maybe it’ll clarify things.

    PHP echo content is sent to the browser, which is received by the script’s .done() callback as data. It’s up to the script to display the returned data in the DOM as appropriate. PHP runs server side, it doesn’t display data directly, it generally sends data to the browser or some similar client side app.

    You cannot directly see PHP echo data from an Ajax request like you would for a new page request because the browser didn’t make a page request, so it’s not expecting a response. It’s all the script’s doing, so the script has to display the response somehow.

    If you want to see PHP debug output for an Ajax request, it can be echoed to the script if the script is configured to handle it. (i.e. receive simple string data and not something like an array) Or PHP can send debug data to the error log. I know a dev who emails debug output to himself. I write debug data to a separate log file because accessing the error logs on my system is a PITA.

    My last .done() suggestion is how the script would display echoed data from PHP. It assumes a string of some sort is being sent, which is what echo does. All good there. It does require the current page to have an DOM element with ID “ajax-info”, for example:
    <div id="ajax-info">Ajax response will be shown here.</div>

    jQuery’s .html() method will replace the content in the div with the Ajax response from PHP.

Viewing 3 replies - 16 through 18 (of 18 total)
  • You must be logged in to reply to this topic.