我正在使用 Webpack 和 Wordpress。
當我在這樣的函式中加入示例 ajax 腳本時:
wp_enqueue_script( 'script-name', get_stylesheet_directory_uri() . '/js/example_script.js', array(), false, true );
wp_enqueue_script( 'general-bundle', get_template_directory_uri() . '/dist/bundle.app.js', array(), false, true );
wp_localize_script('script-name', 'fih_ajax', array( 'url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce(‘example-nonce')));
有用。
問題是當我從functions.php中洗掉example_script.js并將其匯入app.js(因為我想將它包含到bundle.app.js中):
import "./example_script.js";
控制臺顯示:
ReferenceError: Can't find variable: fih_ajax
example_script.js 部分:
$.ajax({
url: fih_ajax.url,
type: 'POST',
data: {
action: 'fimgh_ajax',
nonce: fih_ajax.nonce,
href: hrefValue //the-data
},
在 bundle.app.js 中:
https://jsfiddle.net/4052vaun/1/
uj5u.com熱心網友回復:
wp_localize_script句柄需要指向一個正在使用的句柄,在本例中為“general-bundle”。
改變
wp_localize_script('script-name', 'fih_ajax', array( 'url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce(‘example-nonce')));
進入
wp_localize_script('general-bundle', 'fih_ajax', array( 'url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce(‘example-nonce')));
解決了這個問題。感謝@HowardE 評論
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/424090.html
標籤:javascript 阿贾克斯 WordPress 网页包 排队
上一篇:根據選擇的單選值進行特定查詢
