【发布时间】:2016-07-21 15:17:04
【问题描述】:
将 wordpress 与 AJAX 结合使用我正在尝试从前端更新附件元数据。由于某些原因,我得到 json 响应“NaN”或 null。这是登录用户的表单,所以我没有使用wp_ajax_nopriv_
在我的 functions.php
add_action('wp_ajax_update_portfolio', 'update_portfolio_function' );
function update_portfolio_function(){
$id = $_POST['pid'];
$title = $_POST['itemtitle'];
$description = $_POST['itemdescription'];
$attachment = array(
'ID' => $id,
'post_title' => $title,
'post_content' => $description
);
// now update main post body
wp_update_post( $attachment );
die();
$response = array('pid'=>$id,'title'=>$title);
echo wp_send_json($response);
exit;
}
在我的 jQuery / AJAX 中我有:
function update_info(id, itemtitle, itemdescription)
{
jQuery.ajax({
method: 'post',
url : ajaxurl,
dataType: "json",
data: {
'action':'update_portfolio_function',
'pid' : id,
'itemtitle' : itemtitle,
'itemdescription' : itemdescription,
},
success:function(data) {
alert(data.pid + data.title); //Damn
},
error: function(errorThrown){
console.log(errorThrown);
}
});
//alert("a");
}
作为回应,我想检查 id 和 title 是否已正确提交。如您所见,我正在使用警报来打印它们。值很好地传递到 jQuery 函数中,但我不认为它们是从我的 php 端收到的(或处理不当),因为我在 data.pid 和 data.title 上得到“NaN”作为响应。你能帮帮我吗?
编辑 我的请求详情
【问题讨论】:
标签: javascript php jquery ajax wordpress