JQuery.ajax没有使用HTTPS

时间:2012-10-24 17:48:24

标签: javascript jquery ajax https

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

我已经检查了我的代码一百万次并尝试了一百万种方法来为ajax查询生成url参数。 (使用//作为亲戚,现在只需将协议https附加到网址的开头。这是我的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访问。有什么想法吗?

2 个答案:

答案 0 :(得分:16)

确定。我搞砸了这个超过4个小时,一旦我在URL的末尾加了一个斜杠,问题就消失了,一切正常。我不知道为什么。 Web服务器/ Web服务不需要斜杠来正常运行,但无论出于何种原因,这就是“修复”它的原因。感谢有用的评论家伙。

答案 1 :(得分: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>

问题在于,请求标题显示referer页面是一个ssl页面,但响应标题显示了一个“http”页面的位置,如上面Rob的代码printscreen。

我开始知道每次从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页面工作正常