【问题标题】:Have url redirection after ajax responseajax响应后有url重定向
【发布时间】:2018-11-23 20:58:31
【问题描述】:

我将以下 php 代码与 ajax 一起使用。一切正常,但不是只提示消息已通过的 ajax 确认消息,我想要一个 url 重定向。 任何帮助将不胜感激。

<?php   
// Check if sent
try {
    $sendmailResult = mail($recipient, $subject, $email_content, $headers);
    if( $sendmailResult === TRUE ) {
            returnAndExitAjaxResponse(

            constructAjaxResponseArray(
            TRUE
            )

        );


    } else {
        returnAndExitAjaxResponse(
            constructAjaxResponseArray(
                FALSE,
                'ERROR_AT_PHPMAIL',
                array('error_information'=> error_get_last() )
            )
        );
    }
} catch (Exception $_e) {
    returnAndExitAjaxResponse(
        constructAjaxResponseArray(
            TRUE,
            'ERROR_AT_PHPMAIL',
            array('error_message'=> $_e->getMessage())
        )
    );
}

/*
    Construct ajax response array
    Input: Result (bool), Message (optional), Data to be sent back in array
*/
function constructAjaxResponseArray ($_response, $_message = '', $_json = null) {
    $_responseArray = array();
    $_response = ( $_response === TRUE ) ? TRUE : FALSE;
    $_responseArray['response'] = $_response;
    if(isset($_message)) $_responseArray['message'] = $_message;
    if(isset($_json)) $_responseArray['json'] = $_json;
    return $_responseArray;
}
/*
    Returns in the Gframe ajax format.
    Input: data array processed by constructAjaxResponseArray ()
    Outputs as a html stream then exits.
*/
function returnAndExitAjaxResponse ($_ajaxResponse) {
    if(!$_ajaxResponse){
        $_ajaxResponse = array('response'=>false,'message'=>'Unknown error occurred.');
    }
     header("Content-Type: application/json; charset=utf-8");
    echo json_encode($_ajaxResponse);
    die();
}
?>

【问题讨论】:

标签: php ajax url redirect


【解决方案1】:

由于它是 ajax,我认为您不能直接从后端调用重定向它。

我能想到的最佳解决方案(当然可能还有其他我不知道的解决方案,但只是给你我的见解)是在 AJAX 响应中返回你想要重定向到的 url,然后使用 document.location.href当您的 AJAX 调用成功时从 javascript 重定向。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    相关资源
    最近更新 更多