8

因此,我使用 .ajax 方法从 jQuery 调用 Web 服务。调用该方法的页面是 HTTPS/SSL 页面,但是当调用时,jQuery 不断发出 HTTP 请求并且失败,因为服务器设置为将所有 HTTP 流量重定向到 HTTPS ......所以 301错误又回来了。

我检查了我的代码一百万次,并尝试了一百万种方法来为 ajax 查询生成 url 参数。(使用 // 相对,现在只是将协议 https 附加到 url 的开头。这是我的 javascript:

function add_inbound_record(serial_number, pass_fail_value)
{
   pfv = pass_fail_value.toUpperCase();
   url = location.protocol + "//" + location.hostname + "/inbound/record-                 inspection/" + serial_number + "/" + pfv;
   $.ajax({
   url:url,
   cache:false,
   });
}

因此,当此代码执行时,我检查了 firebug 中的 url 参数,它正确显示为 https 并且 URL 格式正确。但是,当我执行 ajax 函数时,我在 firebug 中看到了这一点:

301 MOVED PERMANENTLY

192.168.1.9

20 B

192.168.1.9:443

Response Headersview source
Connection  keep-alive
Content-Encoding    gzip
Content-Length  20
Content-Type    text/html; charset=utf-8
Date    Wed, 24 Oct 2012 17:33:34 GMT
Location    http://192.168.1.9/inbound/record-inspection/011234567890123421000000002995/P/?_=1351100020609
Server  nginx/1.1.19
Vary    Accept-Encoding

Request Headersview source
Accept  */*
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Cookie  djdt=hide; csrftoken=sF9RUxrlS6IKURxOryH2d2yT05gMighn;         sessionid=9682390da4011e445931643c81be9aae
Host    192.168.1.9
Referer https://192.168.1.9/fingerprint/inspect/
User-Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:15.0) Gecko/20100101     Firefox/15.0.1
X-Requested-With    XMLHttpRequest

正如您在上面从引用者看到的那样,协议是 HTTPS 但响应标头中的位置是 HTTP?我一生都无法弄清楚为什么请求以 HTTP 而不是 HTTPS 的形式通过网络传输。考虑到 301 响应是作为 HTTP 进行的,因此它是准确的,因为再次将网络服务器配置为仅允许 HTTPS 访问。有任何想法吗?

4

3 回答 3

26

行。我把这个搞砸了 4 个多小时,只要我在 URL 的末尾添加一个斜杠,问题就消失了,一切正常。我不知道为什么。Web 服务器/Web 服务不需要斜杠即可正常运行,但无论出于何种原因,这就是“修复”它的原因。感谢您提供有用的评论。

于 2012-10-24T21:57:08.573 回答
1

对于同样的问题,我也很沮丧。我正在从我的 ssl 页面发送 ajax 请求,如下所示:

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || 

$_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";

<script type="text/javascript">
    $.ajax({ 
          url: "<?php echo $protocol.$_SERVER['HTTP_HOST'].$this->url(array("action"=>"autocomplete", "controller"=>"ajax", "module"=>"default"));?>",
                    data: { term: $("#keyword").val()},
                    dataType: "json",
                    type: "POST",
                    success: function(data){
                        response(data);
                    }
                });

</script>

问题是,请求标头显示引用页面是 ssl 页面,但响应标头显示位置为“http”页面,如 Rob 的代码打印屏幕上方所示。

我开始知道,每次当您从 ssl 页面响应发出 ajax 请求时,都会到达同一个页面,即 ssl 页面,而当您通过响应从非 ssl 页面发出 ajax 请求时,也会到达相同的页面,即非ssl页面。这是 ajax 请求和响应的默认规则。

我认为,我的代码方面肯定存在问题,它在从 https 发送时强制从 http 做出响应。没错,我的怀疑是对的。实际上有一个默认代码强制重定向到响应 http 页面而不是 https。我正在分享以前的代码:

    class Custom_Customplugins extends Zend_Controller_Plugin_Abstract
    {
        public function preDispatch(Zend_Controller_Request_Abstract $request)
        {
        $action = $request->getActionName();
        $controller = $request->getControllerName();
        $module = $request->getModuleName();

        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $host = $_SERVER['HTTP_HOST'];
        if($host != "www.xyz.com")
        {
            if($protocol == "http://")
            {

            }
        }
        else
        {
            $r = new Zend_Controller_Action_Helper_Redirector();
            $u = new Zend_Controller_Action_Helper_Url();
            if(
            ($action == "index" && $controller == "index" && $module == "default") 
            || ($action == "login" && $controller == "index" && $module == "default")
            || ($action == "businessownerregistration" && $controller == "index" && $module == "default")
            || ($action == "customerregistration" && $controller == "index" && $module == "default")
            || ($action == "index" && $controller == "changepwd" && $module == "admin") 
            || ($action == "index" && $controller == "businessowner" && $module == "businessowner") 
            || ($action == "changepwd" && $controller == "serviceprovider" && $module == "businessowner")
            || ($action == "index" && $controller == "customer" && $module == "default")    
              )
            {
            if($protocol == "http://")
            {
                $r->gotoUrl('https://'.$host.$u->url(array("action"=>$action, "controller"=>$controller, "module"=>$module)))->redirectAndExit();
            }
            }
            else
            {
            if($protocol == "https://")
            {
                $r->gotoUrl('http://'.$host.$u->url(array("action"=>$action, "controller"=>$controller, "module"=>$module)))->redirectAndExit();
            }
            }
        }
        }
    }

改正后代码如下:

<?php
    class Custom_Customplugins extends Zend_Controller_Plugin_Abstract
    {
        public function preDispatch(Zend_Controller_Request_Abstract $request)
        {
        $action = $request->getActionName();
        $controller = $request->getControllerName();
        $module = $request->getModuleName();

        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $host = $_SERVER['HTTP_HOST'];
        if($host != "www.xyz.com")
        {
            if($protocol == "http://")
            {

            }
        }
        else
        {
            $r = new Zend_Controller_Action_Helper_Redirector();
            $u = new Zend_Controller_Action_Helper_Url();
            if(
            ($action == "index" && $controller == "index" && $module == "default") 
            || ($action == "login" && $controller == "index" && $module == "default")
            || ($action == "businessownerregistration" && $controller == "index" && $module == "default")
            || ($action == "customerregistration" && $controller == "index" && $module == "default")
            || ($action == "index" && $controller == "changepwd" && $module == "admin") 
            || ($action == "index" && $controller == "businessowner" && $module == "businessowner") 
            || ($action == "changepwd" && $controller == "serviceprovider" && $module == "businessowner")
            || ($action == "index" && $controller == "customer" && $module == "default")    
              )
            {
            if($protocol == "http://")
            {
                $r->gotoUrl('https://'.$host.$u->url(array("action"=>$action, "controller"=>$controller, "module"=>$module)))->redirectAndExit();
            }
            }
            else if(
                ($action == "autocomplete" && $controller == "ajax" && $module == "default")
                || ($action == "refreshcaptcha" && $controller == "index" && $module == "default")
               )
            {

            }
            else
            {
            if($protocol == "https://")
            {
                $r->gotoUrl('http://'.$host.$u->url(array("action"=>$action, "controller"=>$controller, "module"=>$module)))->redirectAndExit();
            }
            }
        }
        }
    }

?>

现在,我的 https 页面工作正常

于 2013-05-16T05:48:05.460 回答
0

这是您在谷歌上搜索此问题时出现的问题之一,因此这可能会对未来的读者有所帮助。

我不知道根本原因,但是根据我自己的经验,每当我多次遇到这个问题时,我的 url 都有很多符号(或分隔符)在一起,只要它试图消除它们就会中断。不知道是故意的还是bug

发生这种情况的示例:(使用 XMLHttpRequest() 但原理相同)

const url_test = `https://foo.bar.faz/api/api_call/${api_parameter}/${api_parameter2}/${params}`;

xhttp.open("GET", url_test, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

console_log(url_test); // https://foo.bar.faz/api/api_call/1/2/?get=param&get2=param2&
xhttp.send(); // http://foo.bar.faz/api/api_call/1/2?get=param&get2=param2& <-- request sent to this URL, causing a mixed content error

为了解决这个问题,我不得不更改 Laravel(因为它是我目前正在使用的框架)路由以添加/ajax到最后,这样在调用时符号&?就不会在一起。

const url_test = `https://foo.bar.faz/api/api_call/${api_parameter}/${api_parameter2}/ajax${params}`;

xhttp.open("GET", url_test, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

console_log(url_test); // https://foo.bar.faz/api/api_call/1/2/ajax?get=param&get2=param2&
xhttp.send(); // https://foo.bar.faz/api/api_call/1/2/ajax?get=param&get2=param2 <-- request sent to this URL

就像我说的,我不知道这是一个错误还是只是一个未记录的行为,但这对我有用,我希望它能帮助其他遇到同样问题的人。

于 2022-01-18T20:07:33.830 回答