我正在尝试在加载特定页面时调用我的Web服务,即“http://thankyou”我尝试使用javascript和WordPress wp_enqueue_script()函数执行此操作。
<?php function myprefix_enqueue_scripts() {
wp_register_script( 'callApi', get_template_directory_uri() . '/wp-content/themes/storeone/js/Wp_callApi.js', array('jquery'), '', true );
wp_enqueue_script('callApi');
}
add_action( 'wp_enqueue_scripts', 'myprefix_enqueue_scripts' );?>
这是Javascript函数
window.addEventListener("load", function() {
CallApi();
});
function CallApi(){
$.ajax({
type: "POST",
url: "My Web-service",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(obj),
success: function (data) {
alert("Call successful");
}
}
)}
感谢页面加载后如何调用我的Web服务? Code Im试图在functions.php文件中运行
<?php function myprefix_enqueue_scripts() {
wp_register_script( 'callApi', get_template_directory_uri() . '/wp-content/themes/storeone/js/Wp_callApi.js', array('jquery'), '', true );
wp_enqueue_script('callApi');
}
if ( is_page("order-received"){
add_action( 'wp_enqueue_scripts', 'myprefix_enqueue_scripts' );
}
?>