Javascript 简单的PHP到Ajax';未登录';错误-如何?
目前,我正在使用它向ajax发送成功或失败:Javascript 简单的PHP到Ajax';未登录';错误-如何?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,目前,我正在使用它向ajax发送成功或失败: $response = array( 'success' => true ); wp_send_json_success($response); 但是,假设用户没有登录,我想在这里结束php函数,并发送错误消息,提示登录模式弹出。我该怎么做?比如: if(!is_user_logged_in)) { wp_send_json_error('not logged in'); die() } 或者我可以使用更常见的json_编码($respon
$response = array( 'success' => true );
wp_send_json_success($response);
但是,假设用户没有登录,我想在这里结束php函数,并发送错误消息,提示登录模式弹出。我该怎么做?比如:
if(!is_user_logged_in)) {
wp_send_json_error('not logged in');
die()
}
或者我可以使用更常见的json_编码($response)
但这个处理程序怎么知道发生了错误呢?如果我将所有成功和错误的选项放入一个数组中,那么如何在返回json响应的同时终止函数?我如何告诉php发送哪个响应
一旦它到达我的ajax调用,我相信我能找到它。这是我读到的:
var dataArray = jQuery.parseJSON(data);
一些例子会很有帮助。提前谢谢你 这应该有效:
if(!is_user_logged_in()) {
$response = array(
'loggedIn' => false,
'alertMsg' => 'not logged in :,-(',
);
} else {
$response = array(
'loggedIn' => true,
'success' => true,
'alertMsg' => 'test!',
);
}
wp_send_json_success($response);
die();
Ajax调用类似于:
jQuery.get('pathToYourJsonFunctionURL', function(data) {
if (data.loggedIn == true) {
jQuery('#divForLoggedInStatus').text('You are logged in');
} else {
jQuery('#divForLoggedInStatus').text('You are NOT logged in');
}
window.alert(data.alertMsg);
});
返回JSON数据时,不使用parseJSON
(数据已经(解析)为JSON)data.loggedIn
与PHP中返回的array('loggedIn'=>值)相同代码>这应该可以:
if(!is_user_logged_in()) {
$response = array(
'loggedIn' => false,
'alertMsg' => 'not logged in :,-(',
);
} else {
$response = array(
'loggedIn' => true,
'success' => true,
'alertMsg' => 'test!',
);
}
wp_send_json_success($response);
die();
Ajax调用类似于:
jQuery.get('pathToYourJsonFunctionURL', function(data) {
if (data.loggedIn == true) {
jQuery('#divForLoggedInStatus').text('You are logged in');
} else {
jQuery('#divForLoggedInStatus').text('You are NOT logged in');
}
window.alert(data.alertMsg);
});
返回JSON数据时,不使用parseJSON
(数据已经(解析)为JSON)data.loggedIn
与PHP中返回的array('loggedIn'=>值)相同代码>您可以使用json\u编码并创建自己的协议,如下所示:
if(isset($_POST['operation']) && $_POST['operation'] == 'is_user_logged_in'){
if(!is_user_logged_in()){
$response->success = false;
$response->msg = "not logged in";
}else{
$response->success = true;
}
echo json_encode($response);
exit;
}
然后在ajax中:
$.post('AJAX-URL-HERE',{"operation":"is_user_logged_in"},function(response){
if(response.success){
//do whatever
}else{
var message = response.msg
//pop up the login modal
}
},'json').error(function(){alert('Call error')});
您可以使用json_编码并创建自己的协议,如下所示:
if(isset($_POST['operation']) && $_POST['operation'] == 'is_user_logged_in'){
if(!is_user_logged_in()){
$response->success = false;
$response->msg = "not logged in";
}else{
$response->success = true;
}
echo json_encode($response);
exit;
}
然后在ajax中:
$.post('AJAX-URL-HERE',{"operation":"is_user_logged_in"},function(response){
if(response.success){
//do whatever
}else{
var message = response.msg
//pop up the login modal
}
},'json').error(function(){alert('Call error')});
我尝试了这一点,只是使用了警报(消息);但由于某些原因,它返回未定义;但由于某些原因,它返回未定义。快速跟进Dan-假设我想使用该警报消息字段传递评论或帖子的总投票数。我尝试将其作为php变量传递,但没有成功。这可能吗?一切都可能!你能把代码(PHP和JS)作为pastebin发送给我吗?或者作为一个新问题发送给我,我可以看看。谢谢你,丹!我如何在stackoverflow之外与您联系?也许你是被雇佣的,而且价格合理?这是pastebin:QuickFollowUpDan-假设我想使用该警报消息字段来传递评论或帖子的总投票数。我尝试将其作为php变量传递,但没有成功。这可能吗?一切都可能!你能把代码(PHP和JS)作为pastebin发送给我吗?或者作为一个新问题发送给我,我可以看看。谢谢你,丹!我如何在stackoverflow之外与您联系?也许你是被雇佣的,而且价格合理?这是粘贴箱: