[我正在尝试从another post中得出一个例子,我没有足够的声誉来评论(这可能比问一个新问题要好)]
我已在login.js
代码中包含以下代码段,如链接示例中所示。
function send_stuff_to_server(){
var data = {
'action': 'handle_login', // missed this same as your action hook wp_ajax_{handle_login}
'security': MyAjax.security // We can access it this way
}
$.post(MyAjax.ajax_url, data, function (callBack) {
console.log(callBack); // Will return nonce
});
}
HTML / PHP前端
按照another example as指南,我只是制作了一个简单的按钮,使我可以测试外部脚本中定义的send_stuff_to_server
函数。
<!--Creating Nonce Here as example-->
<input type="hidden" name="login_nonce" value="<?= wp_create_nonce('login_nonce'); ?>"/>
<input id="clickMe" type="button" value="clickme">
<script>
//- Using a function pointer:
document.getElementById("clickMe").onclick = send_stuff_to_server;
//- Using an anonymous function:
//document.getElementById("clickMe").onclick = function () { alert('hello!'); };
</script>
当我单击按钮并检查控制台时,出现错误提示$是一个问题。
我已经尝试使用function send_stuff_to_server($)
传递与jQuery相关的“东西”,但仍然不满意。
我不想使用匿名函数,然后在外部脚本(such as this)中定义按钮onclick动作。我只想用$解决JQuery命名空间问题,然后可以像我正在做的那样在字体结束代码中定义on click方法。
$.post
替换为jQuery.post
。