我是jQuery的新手,并且已经正确地将jQuery库包含到我的Wordpress中,但是,似乎无法在我的Wordpress中执行jQuery调用,看不到任何结果。以下是我的代码片段:
/* In my html, initiate call when selecting a different member id */
<script type="text/javascript">
$(document).ready(function() {
$("#select_member_id").change(function() {
$.getJSON("<?php bloginfo('template_directory') ?>/get_member_info.php", {"member_id" : $(this).val()}, function(jMember,status) {
member = JSON.parse (jMember);
$("#member_firstname").text(member.first_name);
});
});
});
</script>
/* In get_member_info.php suppose to return a JSON result */
<?php
include("member_roster.php"); // this is just an array file
$member_id = $_GET["member_id"];
echo "{" . '"'.'first_name'.'"' . " : " . $member_roster[$member_id] ["first_name"] . "}";
?>
请告诉我我做错了什么......感谢很多人!
答案 0 :(得分:0)
使用$.getJSON
时,数据已经过解析,因此请勿调用JSON.parse
。
$.getJSON("<?php bloginfo('template_directory') ?>/get_member_info.php", {"member_id" : $(this).val()}, function(member,status) {
$("#member_firstname").text(member.first_name);
});
手工构建json是一个坏主意,你应该使用json_encode
echo json_encode(array('first_name' => $member_roster[$member_id] ["first_name"] ));