添加了视图页面中显示的JS文件,但单击时出现404错误

Added JS file shown in view page, but gives 404 error when clicked

本文关键字:单击 错误 文件 JS 视图 显示 添加      更新时间:2023-09-26

在子主题的函数中创建以下代码后.php,

add_action('wp_enqueue_scripts', 'js_files');
function js_files() {
wp_register_script('ajax_call_mkto', get_template_directory_uri() . 'js/something.js', array('jquery'), false);
wp_enqueue_script('ajax_call_mkto');

}

检查了查看页面源代码并单击链接以查看它是否有效,单击它后,它给了我一个 404 未找到页面。我的JS文件有问题吗?

var $k = jQuery.noConflict();
$k('#something .somethinghere("yolo")').click(function() {
var $this = $k(this);
// Ajax call
ajaxConnection($this);
});
function ajaxConnection(item) {
alert('IT WORKS!');
}

这是我在控制台日志中收到的错误:

404(未找到) somewebsite.com/:79 资源解释为图像,但使用 MIME 类型文本/html 传输


更新

我想通了。

我的脚本文件夹位于子主题目录中。我必须使用"get_stylesheet_directory_uri()"才能从子主题中获取折叠,而不是使用从父主题文件夹中获取目录的"get_template_directory_uri()"。

感谢您的帮助!这是我的小错误。

> Waaaay太复杂了。试试这个:

wp_enqueue_script('ajax_call_mkto', get_template_directory_uri() . 'js/something.js');

删除不需要的wp_register_script();