首页 > 解决方案 > 在这种情况下,为什么在 AJAX 发布后,URL 会重定向到带有 POST 参数(如 GET 参数)的 URL?

问题描述

插件模块 Controller.php中:

我附加了这个order_detail功能:

    /**
     * @param $vars
     */
    public function order_detail($vars){

        $modulelink = $vars['modulelink']; // eg. addonmodules.php?module=addonmodule
        $version = $vars['version']; // eg. 1.0
        $LANG = $vars['_lang']; // an array of the currently loaded language variables

        $orderid = 28;//isset($_REQUEST['orderid']) ? $_REQUEST['orderid'] : '';

        $sql = "SELECT tblhosting.id, tblhosting.regdate, tblhosting.domain, tblhosting.billingcycle, tblhosting.nextduedate, tblhosting.nextinvoicedate, tblhosting.termination_date, tblhosting.domainstatus, tblhosting.username, tblhosting.notes, tblhosting.dedicatedip, tblhosting.assignedips FROM tblhosting ";
//        $sql .= "LEFT JOIN tblorders ON ";
//        $sql .= "tblhosting.orderid = tblorders.id ";
        $sql .= ("WHERE tblhosting.orderid = " . $orderid);

        $tblhostings = Capsule::select($sql);

        $table_tbody = "";
        for($i = 0; $i < count($tblhostings); $i ++) {

            $item = $tblhostings[$i];

            $row = "<tr>";

            $row .= ("<td><input type='text' id='tblhosting-id' name='id' class='form-control' readonly=\"readonly\" style='width:80px;' value='" . $item->id . "'></td>");
            $row .= ("<td><textarea id='tblhosting-domain' name='domain' class='form-control' style='width:320px;'>" . $item->domain . "</textarea></td>");
            //$row .= ("<td>" . $item->billingcycle . "</td>");
            $row .= ("<td ><div style='width:80px;'>" . $item->nextduedate . "</div></td>");
            $row .= ("<td ><div style='width:80px;'>" . $item->nextinvoicedate . "</div></td>");
            //$row .= ("<td>" . $item->termination_date . "</td>");
            //$row .= ("<td>" . $item->username . "</td>");

            $row .= ("<td><textarea id='tblhosting-dedicatedip' name='dedicatedip' class='form-control' style='width:200px;'>" . $item->dedicatedip . "</textarea></td>");
            $row .= ("<td><textarea id='tblhosting-assignedips' name='assignedips' class='form-control' style='width:200px;'>" . $item->assignedips .  "</textarea></td>");
            $row .= ("<td>" . $item->domainstatus . "</td>");
            $row .= ("<td><div style='width:80px;'>" . $item->regdate . "</div></td>");
            $row .= ("<td><textarea id='tblhosting-notes' type='textarea' name='notes' class='form-control' style='width:200px;'>" . $item->notes .  "</textarea></td>");

            $row .= ("<td> <button id='modify' type='submit' class='btn btn-warning'>modify</button>  <button id='send-email' type='button' class='btn btn-success'>Send email</button>  </td>");

            $row .= "</tr>";

            $table_tbody .= $row;

        }


        $div = "<div class='container'><div><h2>order ID: {$orderid} :</h2></div>";

        $div .= "<div >";

        $div .= "<form '><table class='table'>";

        $div .= "<thead><tr>";

        $div .= "<th>product ID</th>";
        $div .= "<th>product name</th>";
        //$div .= "<th>billingtype</th>";
        $div .= "<th>nextdue</th>";
        $div .= "<th>nextvoice</th>";
        //$div .= "<th>terminate time</th>";
        //$div .= "<th>username</th>";

        $div .= "<th>dedicated IP</th>";
        $div .= "<th>assigned IP</th>";
        $div .= "<th>status</th>";
        $div .= "<th>regdate</th>";
        $div .= "<th>note</th>";
        $div .= "<th>ope</th>";

        $div .= "</tr></thead>";

        $div .= "<tbody>";
        $div .= $table_tbody;
        $div .= "</tbody>";

        $div .= "</table>";

        $div .= "</div></div>";

        $js = "<script>";
        $js .= "

            var origin_url = window.location.href;//\"http://192.168.33.10/whmcs-nlidc.com/whmcs/admin/addonmodules.php?module=addonmodule&action=order_detail&orderid=100\";
            var url_array =origin_url.split(\"/admin/\")
            var url = url_array[0] + \"/admin/api/admin_apis.php\";



            jQuery(document).ready(function(){
                $('#modify').click(function(){
                    //alert('modify');

                    var id = $('#tblhosting-id').val();
                    var domain = $('#tblhosting-domain').val();
                    var dedicatedip = $('#tblhosting-dedicatedip').val();
                    var assignedips = $('#tblhosting-assignedips').val();
                    var notes = $('#tblhosting-notes').val();

                    var params = {
                        admin_api:'update_tblhosting_support',
                        id: id,
                        domain: domain,
                        dedicatedip: dedicatedip,
                        assignedips: assignedips,
                        notes: notes,

                    }

                    $.post(url, params,function(data,status){
                        //alert(\"Data: \" + data + \"nStatus: \" + status);

                        var data_obj = JSON.parse(data);

                        if (status == 'url' && data_obj.status == 200) {
                            alert('update success');

                            alert(window.location.href(www.baidu.com);)
                        }else {
                            //alert('update fail');
                        }
                    });

                })
            });

            jQuery(document).ready(function(){
                $('#send-email').click(function(){
                    alert('send email');
                })
            });

        ";
        $js .= "</script>";


        $html = $div . $js;
        return <<<EOF
            $html
EOF;


    }
}

在此处输入图像描述

我想用这种方式来更新tblhosting条目。您看到我的 jQuery 代码想要使用post方法来更新tblhosting条目。

当我点击更新按钮时,它会执行更新 url,但是它会重定向到这个 URL

http://192.168.33.10/whmcs/admin/addonmodules.php?id=28&domain=10.93.240.193-194(15M%29+HK-Z7-A15++E5-2650%282CPU%29%2F32G%2F500G+ssd%2F++&dedicatedip=10.93.240.193-194&assignedips=10.93.240.193%0D%0A10.93.240.194&notes=02442%2F66655%0D%0A10&intellisearch=1&token=ac271aed87528e68969608329e8a1f4d2a35c858&value=

在此处输入图像描述


我们知道ajax不会刷新整个页面,并且在请求后默认不会重定向,为什么会有不同?

谁知道避免重定向的方法?


我也试过用

window.location.href(origin_url);

在错误的重定向后重定向到后面,但它不会执行。

标签: javascriptphpjquery

解决方案


推荐阅读