前端未定义ajaxurl
-
-
查看本教程.它可能会帮助您.http://www.1stwebdesigner.com/implement-ajax-wordpress-themes/Check this tutorial. It may help you. http://www.1stwebdesigner.com/implement-ajax-wordpress-themes/
- 0
- 2015-06-03
- Nilambar Sharma
-
3 个回答
- 投票数
-
- 2015-06-03
在后端,WordPress本身定义了全局
ajaxurl
变量.此变量不是由WP在前端创建的.这意味着,如果要在前端使用AJAX调用,则必须自己定义此类变量.
执行此操作的好方法是使用
wp_localize_script
.假设您的AJAX调用位于
my-ajax-script.js
文件中,然后为该JS文件添加wp_localize_script,如下所示:function my_enqueue() { wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery') ); wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); } add_action( 'wp_enqueue_scripts', 'my_enqueue' );
本地化JS文件后,可以在JS文件中使用
my_ajax_object
对象:jQuery.ajax( { type: "post", dataType: "json", url: my_ajax_object.ajax_url, data: formData, success: function(msg){ console.log(msg); } });
In backend there is global
ajaxurl
variable defined by WordPress itself.This variable is not created by WP in frontend. It means that if you want to use AJAX calls in frontend, then you have to define such variable by yourself.
Good way to do this is to use
wp_localize_script
.Let's assume your AJAX calls are in
my-ajax-script.js
file, then add wp_localize_script for this JS file like so:function my_enqueue() { wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery') ); wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); } add_action( 'wp_enqueue_scripts', 'my_enqueue' );
After localizing your JS file, you can use
my_ajax_object
object in your JS file:jQuery.ajax( { type: "post", dataType: "json", url: my_ajax_object.ajax_url, data: formData, success: function(msg){ console.log(msg); } });
-
不用使用wp_enqueue_scritp就能使用wp_localize_script吗?can I use `wp_localize_script` without having to use `wp_enqueue_scritp`?
- 2
- 2015-06-03
- dread_cat_pirate
-
您在wp_localize_script中使用脚本句柄,因此必须对至少一个脚本使用wp_enqueue_script.但是...不使用wp_enqueue_script不是一个好主意(您可能会遇到一些冲突和依赖问题).You use script handle in wp_localize_script, so you have to use wp_enqueue_script for at least one of your scripts. But... Not using wp_enqueue_script is not a good idea (you risk some conflicts and dependencies problems).
- 1
- 2015-06-05
- Krzysiek Dróżdż
-
我没有要加载的任何外部脚本,我只想使用ajaxurl进行ajax调用.那不可能吗?i don't have any external script to load, i just want to use ajaxurl to make an ajax call. is that not possible?
- 0
- 2015-09-23
- R T
-
您将把这个AJAX呼叫放在哪里?作为内联脚本?这是一个非常糟糕的主意...And where will you put this AJAX call? As inline script? It's a very bad idea...
- 0
- 2015-09-23
- Krzysiek Dróżdż
-
我有一个单独的表单,因为我要管理验证并在提交时进行ajax调用,当然要通过添加钩子以wordpress方式提交表单.无论如何,我已经找到了使用ajaxurl的方法.i've a separate form, in that i'm managing validation and on submit, an ajax call to submit form with of course wordpress way by adding hook. anyway I've figured out the way for using ajaxurl.
- 0
- 2015-09-23
- R T
-
太棒了...我在前端的Javascript中使用它,就像一个超级魅力:)Awesome... I'm using it in my Javascript on front end and works like a charm :)
- 0
- 2017-10-17
- Omer
-
- 2015-09-23
要直接使用ajaxurl,请在您的插件文件中添加以下内容:
add_action('wp_head', 'myplugin_ajaxurl'); function myplugin_ajaxurl() { echo '<script type="text/javascript"> var ajaxurl = "' . admin_url('admin-ajax.php') . '"; </script>'; }
然后您可以将
ajaxurl
用于ajax请求.to use ajaxurl directly, in your plugin file add this:
add_action('wp_head', 'myplugin_ajaxurl'); function myplugin_ajaxurl() { echo '<script type="text/javascript"> var ajaxurl = "' . admin_url('admin-ajax.php') . '"; </script>'; }
you can then use the
ajaxurl
for ajax request.-
这个答案使" ajaxurl"与默认用法类似.这比公认的答案要好得多.This answer makes `ajaxurl` similarly the same as the default usage. Which is much better than the accepted answer.
- 2
- 2018-12-25
- Abel Melquiades Callejo
-
是的,但是如果您在.js文件中使用它,则没有用.true, but it's useless if you are using it in a .js file.
- 0
- 2019-03-20
- Jules
-
@Jules`ajaxurl`在* .js文件中仍然可用.为此,您可能需要在页面加载的早期声明`ajaxurl`变量.要考虑的另一件事是调用您的外部`* .js`文件.外部文件应称为** AFTER **之后,`ajaxurl`已被实例化,并具有正确的URL值.@Jules `ajaxurl` is still available in a `*.js` file. To do so, you may need to declare the `ajaxurl` variable early on of the page load. Another thing to consider is the calling of the external `*.js` file of yours. The external file should be called **AFTER** the `ajaxurl` has been instantiated and be given the right URL value.
- 1
- 2019-11-09
- Abel Melquiades Callejo
-
控制台有什么错误?what's an error in console?
- 0
- 2020-01-29
- Dharmishtha Patel
-
- 2020-01-28
我在wordpress网站中使用了以下代码.
我们可以这样使用下面的代码来设置ajaxurl.<?php echo esc_url(admin_url('admin-ajax.php')); ?>
我还添加了ajax示例,我们可以在上面的行中使用.
function setNotificationRead() { fetch('<?php echo esc_url(admin_url('admin-ajax.php')); ?>', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' }, body: `action=yourFunctionsAction`, credentials: 'same-origin' }).then(response => { return response.json(); }).then(data => { if (data.status === 'true') { console.log('Do something...'); } }); }
i have use below code in wordpress site.
we can use below code for setup ajaxurl like this.<?php echo esc_url(admin_url('admin-ajax.php')); ?>
i have also added ajax example were we can use above line.
function setNotificationRead() { fetch('<?php echo esc_url(admin_url('admin-ajax.php')); ?>', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' }, body: `action=yourFunctionsAction`, credentials: 'same-origin' }).then(response => { return response.json(); }).then(data => { if (data.status === 'true') { console.log('Do something...'); } }); }
我正在尝试在正面创建一个ajaxform.我正在使用代码
我遇到错误
虽然在管理后端使用类似的代码也可以.我必须使用哪个网址来处理ajax请求?