Support » Developing with WordPress » 500 Error ajax response after database query

  • Hi,
    Any help with the following issue will be greatly appreciated.

    I have been encountering a 500 : Internal Server Error after requesting data from the database from an ajax call.

    I am using admin-amax.php enqueueing it as follows:

    $my_handle = array(
    			'ajaxurl' => admin_url( 'admin-ajax.php' )
    	wp_localize_script( 'javascript', 'ajax-access', $my-handle );

    My ajax request:

    	    url: ajax-access.ajaxurl,
    	    type: 'POST',
    	    async: false,
    	    dataType: 'json',
    	    data: {
    	    	sentvariable: true,
    	    success: function (data) {
    	    error: function (data) {
                    //always run this section whenever db is queried
    	    	alert('ERROR: ' + data.status + ' :: ' + data.statusText);

    If I remove the database query from the php file and send a hard coded string response I get that string as my response in the js file. Everything works fine as long as I don’t query the database on that call.

    • This topic was modified 8 months, 1 week ago by  hmonlouis.

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • We’d need to see the code for the SQL query, and probably the whole function, to see if there’s an error with that part of it. If it’s big, you can use PasteBin to paste it into.

    Thank you catacaustic for your quick reply,

    I have no custom query I’m using a predefined wp function. I also tried a custom query with the same results. I get that error with the following queries/wp-functions:

    $postid = $wpdb->get_var( 'SELECT ID FROM '.$wpdb->prefix.'posts WHERE post_title = "' .$title.'"' );
    $message = wp_insert_post($postarr, true);

    They all work fine on the php side but if i try sending a response back to the calling ajax is where i get the 500 Internal Server Error.

    Here is the function:

    function check_login(){
    // 	global $wpdb;
    		$isloggedin = true;
    		$isloggedin = false;
    	wp_send_json( array(
    			'loggedin' => __( apply_filters( 'the_content', $isloggedin), 'easybulletin' ),
    // 	$str = json_encode(array('loggedin' => $isloggedin));
    // 	echo $str;
    • This reply was modified 8 months, 1 week ago by  hmonlouis.

    There doesn’t look like there’s anything wrong with that from what you’ve posted here.

    Have you checked the servers error logs to see what they have recorded? The Apache error log might have something, and if it’s a database problem the MySQL server logs should show something.

    The logs show nothing that would indicate an error. I checked apache error and access logs and mysql error log.Nothing except the 500 error in the apache access log.

    I see a 400 error coming up with no other explanation:
    "GET / HTTP/1.0" 400 983
    don’t know if that has anything to do with it.

    • This reply was modified 8 months, 1 week ago by  hmonlouis.

    Without seeing errors it’s going to be almost impossible to tell what’s wrong.

    The only things that I’d suggest from here are to try a different database call, something that you know will run fast, and see if that has the same issue. If it does, its not the database, but something in the code that’s between there and the AJAX response. If it runs fine, check the query in phpMyAdmin and see what happens.

    I’ve just snee one other thing that could be an issue.

    Where do you set the action for your AJAX call? On this page, it shows the action paramater as something that’s required when you send an AJAX request. I can’t see that in your request, and without that being set, WordPress won’t know what to do with the request, so it will throw errors.

    Without an action specified in your AJAX request your code shouldn’t be running at all. Have you edited admin-ajax.php directly?

    @catacaustic: I added the action in my enqueue scripts.
    @jacob: I have not edited the admin-ajax.php at all.
    Funny thing is that I have other ajax calls and the do sens back appropriate responses but my check_login() and save_post() functions are the ones with this issue. I have tried the query in phpMyadmin and it runs fine.

    • This reply was modified 8 months, 1 week ago by  hmonlouis.
    Moderator bcworkz


    Your action parameter has to be part of the data: array declared in your $.ajax call. It doesn’t make sense to declare it when enqueuing. Your Ajax handler function must then be hooked into an action whose tag is in part composed of the action value.

    data: {
      sentvariable: true,
      action: "my_ajax"

    Then in PHP:
    add_action('wp_ajax_my_ajax', 'check_login');

    Incidentally, the above action will only fire if the user is logged in. It doesn’t hurt to check again, but it’s not necessary.

    If your Ajax call is going to alter the DB, such as by calling save_post(), you should also pass a security nonce which is verified in PHP, along with verifying the referrer is your page making the Ajax request and not some hacker’s computer sending forged requests.

    In addition to the resource catacaustic linked to, you would do well to review the Plugin Handbook pages on Ajax.

    One last thing that is not causing your problem but should be addressed. There is no point in using the translation function __() on the return from apply_filters() or any variable value. Translation is only possible if the original string is hardcoded. You can do this:
    echo __('Please translate this!', 'my_plugin');
    Not this:
    echo __( $the_content, 'my_plugin');

    @bcworks Thank you for the tip. It helped me figure out what the issue was. I was getting this error because I was trying to modify the database while not signed in, however I am developing a front-end plugin and will always get the 500 error on is_user_logged_in() and my save_post() function.

    I also made the changes you suggested and will work around the 500 error.

    THANK YOU ALL!! for your help.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘500 Error ajax response after database query’ is closed to new replies.