Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax success message returns HTML of my homepage

Using ajax and wordpress (trying to do it properly). It is rather new to me but I had this working before with more flair. Seemingly-randomly started having issues so I broke my code down to the most basic level, and I can't even get that to work!!! I feel like i'm just getting frustrated and it's forcing me to miss a simple mistake. Is there anything wrong with this?

My jQuery:

$.post(
    ajaxurl, // http://localhost/mysite/wp-admin/admin-ajax.php
    {action: "post-save"},
    function(response){
      alert(response);
    }
);

My PHP:

function update_post(){
    echo json_encode(array("success" => "all systems go"), JSON_FORCE_OBJECT ); 
    exit;
}
add_action( 'wp_ajax_post-save', 'update_post' );

The end result is the alert works, which means $.post is successful (right?), but the returned variable response is the html source of my homepage...

like image 793
jamil Avatar asked Mar 29 '13 18:03

jamil


People also ask

Is ajax successful deprecated?

Yes, it is deprecated in jQuery 1.8 onwards. You should use . done() and use . fail() to catch the errors.

How do I return data after ajax call success?

You can store your promise, you can pass it around, you can use it as an argument in function calls and you can return it from functions, but when you finally want to use your data that is returned by the AJAX call, you have to do it like this: promise. success(function (data) { alert(data); });

How do I know if ajax request is successful?

You can check when exactly it returns "success" : // If successful, handle type chaining if ( status >= 200 && status < 300 || status === 304 ) { ... // If not modified if ( status === 304 ) { statusText = "notmodified"; ... // If we have data } else { try { ...

What triggers ajax success?

AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.


1 Answers

I figured it out...

This might help some newbie like myself who makes the same mistake so I'll answer my own question :)

Turns out I wasn't logged in, go figure. The response was strange though and helped to throw me off, returning the entire homepage. The reason for this is I had blocked my functions files which includes the Ajax responder like so:

function block_users()
{
    if( !current_user_can( 'delete_pages' ) ) {  
        wp_redirect( get_home_url(), 301 );
        exit;
    }
}
add_action('admin_init','block_users');

I should have known to log in (thought I was) but, such unexpected results and it was friday :)

Also note to anyone who stumbles onto this: if you do want someone to do ajax without being logged in use the no-priviledge version of wp_ajax, wp_ajax_nopriv.

add_action( 'wp_ajax_nopriv_action', 'function' );

In lieu of

add_action( 'wp_ajax_action', 'function' );

The former does not require permission, ie being logged in, to do ajax requests.

like image 189
jamil Avatar answered Sep 24 '22 02:09

jamil