我刚刚从 jQuery 的 .ajax 函数中收到了这个 json
string(626) "[{"ID":"6032","post_author":"2276","post_date":"2011-07-27 00:00:00","post_date_gmt":"2011-07-27 00:00:00","post_content":"","post_title":"Web Developer\/Programmer","post_excerpt":"","post_status":"publish","comment_status":"closed","ping_status":"closed","post_password":"","post_name":"web-developerprogrammer","to_ping":"","pinged":"","post_modified":"2012-05-03 09:25:32","post_modified_gmt":"2012-05-03 13:25:32","post_content_filtered":"","post_parent":"4459","guid":"http:\/\/startupchile.dev\/joinastartup\/web-developerprogrammer\/","menu_order":"0","post_type":"jobman_job","post_mime_type":"","comment_count":"0"}]"
如何我可以显示 html 的一些参数?例如仅显示 ID,post_title ?
这是我的代码:
(function($){
$('#searchjob').keypress(function(e){
var search = $.trim($(this).val());
if (e.which == "13") {
$.ajax({
beforeSend: function(){
//$('#loading').html(<img src="rutagif" alt="loading" />);
},
url:"../wp-admin/admin-ajax.php",
data:{ action: 'searching_job',data : search },
dataType: "json",
type: "POST",
success: function(data){
console.log(data);
}
});
e.preventDefault();
};
});
})(jQuery);
我可以修复我的错误。来自 PHP 的 json 是错误的(我习惯于 var_dump,现在用于 print_r)。我只是分享成功的代码。
(function($){
$('#searchjob').keypress(function(e){
var search = $.trim($(this).val());
if (e.which == "13") {
$.ajax({
beforeSend: function(){
//$('#loading').html();
},
url:"../wp-admin/admin-ajax.php",
data:{ action: 'searching_job',data : search },
//dataType: "json",
type: "POST",
success: function(data){
var jsonresult = JSON.parse(data);
$.each(jsonresult, function(i, item){
alert(jsonresult[i].post_title);
});
}
});
e.preventDefault();
};
});
})(jQuery);