it needs a full URL because even though it may be the right location relatively on the back end, it’s not on the same on the front end, at the URL where your page is ultimately served.
you’re on the right path with wp_localize_script
. you want to enqueue your ajax script, then pass the admin ajax url to wp_localize_script
:
function my_init_method(){
wp_enqueue_script( 'my-ajax-request', plugin_dir_url( __FILE__ ) . 'js/my_ajax_script.js', array( 'jquery' ) );
wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action('init', 'my_init_method');
now within your ajax script you can refer to MyAjax.ajaxurl
for the URL.
Check this post for a great writeup on properly using ajax in plugins and themes, this is what the WP Codex links to as an example.
Related Posts:
- ajaxurl not defined on front end
- Using Ajax call in jQuery doesn’t work in widget
- Admin-ajax.php appending a status code to ajax response
- how to search users by ajax live search
- jquery & ajax sending data to php
- Ajax submit result opens in admin-ajax.php
- insert query on a custom table using ajax with jQuery plugin Jeditable
- Ajax: Populate with content from a post’s ID not working – duplicating current page html instead
- Data not insert and update through ajax and jQuery in admin page?
- WP ajax requests not stacking?
- ajax working when function is on child theme but not in plugin page
- Ajax +wordpress onClick link redirect to new page and create html content
- Get cat parameter from admin-ajax
- Ajax on the Administration Side of plugin – returns 0
- Jquery php request is returning a weird result
- WordPress Ajax not returning Response
- wp_ajax add_action fuction won’t fire on custom jQuery action
- Filterable posts using categories
- How to get values from Tinymce visual editor popup?
- WordPress function is not called and ajax return 0
- Ajax call not working with
- Page reload occurs before request finishes
- Why does WordPress add 0 (zero) to an Ajax response?
- How to include jQuery and JavaScript files correctly?
- How Do I Use jQuery UI In My Plugin
- Saving data-URI to media library
- Nonces can be reused multiple times? Bug / Security issue?
- How can I run AJAX on a button click event?
- How to Add a .js file Only in one specific Page Dynamically to Head
- wp_localize_script $handle
- Help adding image upload functionality to widget
- How to enable users to down-vote in this simple voting counter (that uses the post meta)?
- How to trap “Publish” button to check for meta box validation?
- Custom plugin giving: wp-admin/admin-ajax.php 400 (Bad Request)
- Use js script from one plugin in another plugin
- Plugin Settings not Saving on Ajax re-ordered table
- Timeout on Admin-Ajax?
- Plugin admin page meta_box toggle and order state not saving
- Comment `Reply` link doesn’t work if comments are loaded from ajax
- How to create popup same as wordpress popup feature
- Only execute jQuery function(on document ready) on the page has shortcode from plugin [duplicate]
- Loading jQuery and jQuery plugin script files correctly [duplicate]
- wp_insert_post() is returning the correct post ID, no failure, but the post content does not get updated
- include wp-blog-header not working on MAMP
- How can I pass get_the_author_meta(‘user_email’) through the REST API?
- Woocommerce checkout update totals with datepicker
- Including the necessary functions for a custom ajax registration form
- get post attachment using ajax
- Dashboard – get status and position of metaboxes and pass them to ajax method
- Ajax contact form widget plugin data not insert in database
- Display WordPress the_content() via data attributes
- Storing data in wordpress database from ajax call from different website
- adjust section according to country?
- javascript datatables in a plugin
- WordPress custom taxonomy check box to dropdown
- Hide shortcodes “code” when using the “editor” the same as wordpress do with “gallery”
- Plugin – Make sure jquery is loaded in my settings page plus my JS file
- should i be checking for jquery before enqueing it in a plugin
- Remove entire [$key] from array stored in custom field using Ajax – unset($array[$key]); not working
- jQuery instantly executes (a button click and css change) on load
- making a plugin that moves other plugins wp_head actions to wp_footer
- Setting a JSON web token from a secondary api as a secure cookie on WordPress
- How do I get rid of my inclusion race-condition on wp_enqueue_script
- Issues Updating Post Meta with AJAX (Seems simple but cannot figure it out)
- AJAX call to admin-ajax.php by subscriber returns home page
- Ajax function is not working on WordPress
- About a programming language starts with [closed]
- Workflow for new importer plugin – your advices?
- Use options to control jQuery plugin
- How to localize data array in plugin’s option page
- AJAX request not routing through proxy
- Ajax request sends url rather than data
- Using jQuery prepend() with file include
- wc_get_template_part doesnt display the content [duplicate]
- Ajax action has 200 status but response of No response data available for this request
- GET request return value as error instead of success
- ajax stopped working when not logged in wordpress
- jQueryUI draggable doesn’t work in WordPress plugin
- setTimeout not working in jquery
- ajax response strips multidimensional array and unable to decode
- 400 Bad Request and illegal invocation in wp_ajax based on processData set to false or true
- Rate limiting ajax requests in WordPress
- Using AJAX to submit and return data inside the WordPress Plugin Boiler Plate framework
- $_SESSION inside php function executed by AJAX
- Two same AJAX calls – one is working, other doesn’t
- Input field duplicates on form submit by jQuery
- PHP includes with AJAX actions
- WP Ajax on page load not working on bluehost but was working on Godaddy
- WordPress ajax not working in registration
- Script work on non logged in user but not work for logged in user?
- Can’t get query string in ajax call
- Cannot echo a JS variable to a jQuery plugin coming from wp_localize_script
- An adiitional function fires on my AJAX submit
- Fatal error: Call to a member function query() on a non-object in my ajaxpage
- How do I use (or mimic) document.getElementById() on a page loaded from WordPress database?
- Jquery post responses 500 error after some time and lastly an 503 error
- How to get the value entered in the input field in wordpres
- Return custom product in ajax call loop
- getJSON response to PHP
- Insert meta-description into Yoast-SEO input-field via JavaScript