2012-10-24 44 views
4

所以,我使用.ajax方法從jQuery調用Web服務。調用方法的頁面是一個HTTPS/SSL頁面,但是在調用時,jQuery不斷髮出HTTP請求,並且由於服務器設置爲將所有HTTP通信重定向到HTTPS而失敗,所以301錯誤正在回來。JQuery.ajax不使用HTTPS

我檢查了我的代碼一百萬次,並嘗試了一百萬種方法爲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, 
    }); 
} 

所以,這個代碼執行的時候,我檢查的螢火網址放慢參數,它顯示了。正確地以https和適當地形成的URL然而,當我執行AJAX功能我看到這在螢火:

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/01123456789/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響應是準確的考慮我t會像HTTP一樣,因爲Web服務器再次被配置爲只允許HTTPS訪問。有任何想法嗎?

+1

的HTTPS請求被重定向到非相當於ssl。檢查你的網絡服務器配置。 Apache(或IIS等)爲請求報告什麼? – Madbreaks

+0

'location.protocol'確實是'https'?當您手動添加https字符串時會發生什麼? – mekwall

+0

您是否嘗試過使用捲曲或類似方法手動測試端點? –

回答

15

好的。我搞砸了4個多小時,只要我在URL的末尾加了斜線,問題就消失了,一切正常。我不知道爲什麼。 Web服務器/ Web服務不需要斜線即可正常運行,但無論出於何種原因,這就是「固定」它的原因。感謝有幫助的評論傢伙。

+0

感謝您的更新。不要忘記接受你的答案。 –

+0

我有同樣的錯誤,似乎沒有文件爲什麼會發生這種情況。 非常感謝,我在這方面花了太長時間。 – sparknoob

+0

你的意思是一個正斜槓像/或反斜槓像\? –

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請求和響應的默認規則。

我想,從我的代碼端肯定有一個問題,強制從http發送響應時從https發送。 確切地說,我的懷疑是對的。其實有一個默認的代碼強制重定向到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頁面工作正常