Last active
December 16, 2015 18:19
-
-
Save interjc/5476330 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//sample ajax function | |
add_action('wp_ajax_sample_ajax_function', 'sample_ajax_function'); //添加当用户处于登录状态下添加响应的 Hook | |
add_action('wp_ajax_nopriv_sample_ajax_function', 'sample_ajax_function'); //添加当用户处于未登录状态下添加响应的 Hook | |
//响应 AJAX 请求的主程序来了 | |
function sample_ajax_function(){ | |
$data = $_POST['data']; //接受并保存 request,这里的 data 可以是其他的东西,取决于你发过来的参数名,这个后面会说 | |
//简单预设将要返回的数据,假定接收到了 data 这个参数就返回 {'status':'success'},否则返回 {'status':'fail'} | |
$arr = array('status'=>'success'); | |
if(!$data) $arr['status'] = 'fail'; | |
//在数据中插入先前发送过来的参数 data | |
$arr['data'] = $data; //实际上这一步是扯淡的,正确的做法当然是处理这个请求了 | |
/* 然后你开始根据接收到的参数做一些 php 该做的事情 */ | |
$response = json_encode($arr); //将上面各步骤生成的数据转换为 JSON 格式 | |
header( "Content-Type: application/json" ); //发送 Header,确定数据包为 JSON 格式 | |
echo $response; //生成数据 | |
exit; //这一步一定要加,要不然 $response 会始终返回 0,有些说法是返回 die(),不过我不太喜欢这个方法名,所以还是 exit 吧 | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var uri = MyAjax.uri, param = { | |
action : 'update_stock', | |
data : {} //这里的 data 可以自己添加任何东西,下面除了 data 外也可以添加别的内容 | |
}, ; | |
//开始发送请求 | |
$.post(uri, param, function(data){ | |
//ajax 返回数据成功后的操作,下面的意思是你可以在 firebug 的 console 里面看见返回的数据,当然也可以进行其他的操作 | |
console.log(data); | |
}, 'json'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php wp_enqueue_script("jquery"); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'uri' => admin_url( 'admin-ajax.php' ) ) ); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
/* <![CDATA[ */ | |
var MyAjax = { | |
uri : "http://example.com/wordpress/wp-admin/admin-ajax.php" | |
}; | |
/* ]]> */ | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
code for http://interjc.net/archives/2013/04/28/ajax-response-in-wordpress-theme.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment