[英]ajax call url wordpress
此文件中content / themes / themename / js / main.js中的js文件,我正在嘗試對該文件content / themes / themename / lib / file.php進行ajax調用
這就是我的ajax調用的樣子
$.ajax({
type: 'POST',
url: 'file.php',
success: function(result) {
var data = jQuery.parseJSON(result);
console.log(data.name);
}
});
但是該網址無法正常工作,有人可以幫助我嗎?
我也嘗試過../lib/file.php
也許您可以嘗試使用相對於根目錄ex的路徑:
$.ajax({
type: 'POST',
url: '/wordpress_site/wp-content/themes/themename/lib/file.php',
success: function(result) {
var data = jQuery.parseJSON(result);
console.log(data.name);
}
});
我通過在header.php文件的body標簽中執行此操作來修復它
data-theme-url="<?php echo get_stylesheet_directory_uri(); ?>
並像這樣在我的javascript中使用URL
url: $('body').data('theme-url') + '/lib/file.php',
$.ajax({
type: 'POST',
url: '<?php echo get_stylesheet_directory_uri();?>/lib/file.php',
success: function(result) {
var data = jQuery.parseJSON(result);
console.log(data.name);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.