Ajax调用不会'没有回应

Ajax call doesn't give response

本文关键字:回应 调用 Ajax      更新时间:2023-09-26

在登录页面中,我包含了一个php文件,它看起来像下面的

JS

 <script>
var APP_URL = '<?= APP_URL?>';
function migrate(value){
    if(value==1) {
       $j.ajax({
            type: 'POST',
            url: APP_URL+"corporate/secApi",
            data : {migration:1},
            success: function (rs) {
                console.log(rs);
                    alert("Migration Completed!'nEnjoy the new Goal feature");
                    $j("#fulldiv").remove();
            },
            failure: function () {
                alert("call to secApi failed!");
            }
        });
    }else{
        $j("#fulldiv").remove();
    }
}
</script>

HTML

<div id="fulldiv">
<div id="dialog">
    <div id="dialog-bg">
        <div id="dialog-title">New Feature</div>
        <div id="dialog-description">EMarketeer has added new feature known as Goals, enabling this feature will allow you to set your own goals and keep the track of progress.</div>
        <div id="dialog-description" style="padding-top: 50px;">Would you like to Migrate to use new Goals feature?</div>
        <!-- Buttons, both options close the window in this demo -->
        <div id="dialog-buttons">
            <a href="javascript:migrate(1)" class="large green box">YES,  do it now!</a>
            <a href="javascript:migrate(0)" class="large red box">NO,  maybe  later</a>
        </div>
    </div>
</div>

PHP

if(isset($_POST['migration'])){
    $response = false;
    if($_POST['migration']==1) {
        $apiMgr = new SecureAPIClient((int)$_SESSION['id_user'], (int)$_SESSION['id_user_sub'], $ALLOWED_RESOURCES);
        $response = $apiMgr->migrateAccount($_SESSION['id_user']);
        if($response==true)
            echo 1;
        return;
    }
}

调用完成后,我可以看到控件来到secApi.php。完成后,日志中没有任何内容。以前它是有效的,但只有当我回显'd1而不是true时,在ajax调用的成功部分,我检查了(rs==1)dosomething是否有效,但现在甚至都不起作用了。我就是找不到原因。

有什么建议吗?我做错什么了吗?

为了在客户端获得该值,您应该在PHP中进行echo而不是return。

<?php
   $response = false;
   if(isset($_POST['migration'])){
        if($_POST['migration']==1) {
            $apiMgr = new SecureAPIClient((int)$_SESSION['id_user'], (int)$_SESSION['id_user_sub'], $ALLOWED_RESOURCES);
            $response = $apiMgr->migrateAccount($_SESSION['id_user']);
        }
   }
   echo $response;
?>