Skip to content
Advertisement

AJAX (admin_url(‘admin-ajax.php’);?action=) Not Found

So for my AJAX tabs I have the following script:

JavaScript

I got following error with url: "wp-admin/admin-ajax.php" and the error is example.com/wp-admin/admin-ajax.php?action=my_tab_menu 404 Not found.

Then I changed it to the following and got the same error: url: "admin_url('admin-ajax.php')" then, example.com/admin_url('admin-ajax.php');?action=my_tab_menu 404 Not found.

What is going on and what am I doing wrong?

Thanks

EDIT

Here is my files:

So I feel like I am really close to getting Ajax working but I am getting an error:

Here is php:

JavaScript

And in my function.php:

JavaScript

And here is my file names:

JavaScript

EDIT 2

I changed the success to alert("Success!"); and I got the Success alert. So everything is working except it is not fetching any data from other php files. What am I missing?

EDIT 3

With console.log(data);, this is the script that I see in the console:

JavaScript

Advertisement

Answer

Then I changed it to the following and got the same error: url: "admin_url('admin-ajax.php')" then, example.com/admin_url('admin-ajax.php');?action=my_tab_menu 404 Not found.

If the URL contains the literal string admin_url('admin-ajax.php'); then that means you PHP isn’t being parsed.

Try:

JavaScript

You can also use wp_localize_script to set the ajax URL when you enqueue a script:

JavaScript

https://codex.wordpress.org/AJAX_in_Plugins#Separate_Javascript_File

In this case you would set the URL like this:

JavaScript

The advantage of doing it this way is that you don’t have to inline your javascript; you can just enqueue a JS file like you normally would.

From the comments:

So, when I went to example.com/wp-admin/admin-ajax.php I get “0” on a blank page. And that is exactly what is shown on the console on the ajax tab page. Is it normal?

Getting a 0 result either means your hook is not attached to the action or your hook generates no output and fails to exit.

In your JS, you’re setting your action like this:

JavaScript

In your PHP your declaring your hooks like this:

JavaScript

You need to either use royal_private_tab or my_tab_menu in both places, ex:

JavaScript

Also, you should exit at the end of your hook:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement