Using Ajax call in jQuery doesn’t work in widget

So I’m in the midst of creating a new conversion widget and I’m having some trouble getting the Ajax call to work in my widget. (works perfectly outside of WordPress).

The problem at the moment is that it outputs the whole homepage in the #results div rather than just the results figure:

2 Answers
2

If your AJAX Url is conversion.php it will most likely point to yourdomain.com/conversion.php.
Is your file in that location?

Looks like not, and I guess you don’t have a 404.php, and in that case your request gets directed to your index.php, that’s why you get the contents of your homepage.

If your file is in your theme folder, you would have to change the URL to wp-content/themes/yourtheme/conversion.php

But the much easier and recommended way is to use WP built in AJAX functionality:
See this codex page for more informations:
http://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)

EDIT: Most posts on this topic are for AJAX on the WordPress backend but it’s possible to use it on the frontend as well.

Leave a Comment