2013-03-13 61 views
1

重定向由於一些要求,我需要登錄到使用jQuery AJAX網站..但是當我嘗試這樣做.. Firebug控制檯是說,它是302錯誤。 我試圖實施其處理,但遺憾的是沒有成功故障處理302與jQuery AJAX

下面的代碼是我的代碼我已經試過至今:

<script type='text/javascript' src='http://code.jquery.com/jquery.js'> 
    </script> 
    <script type='text/javascript'> 


    $(document).ready(function(){ 


    $.ajax({ 

    url  : "https://testDomain/login.asp", 
    async  : false, 
    type  : 'POST', 
    data  : {Username:'test',Password:'testPass'}, 
    success : function(respText){alert(respText);}, 

    error  : function (jqXHR, textStatus, errorThrown) 
       { 
        console.log(jqXHR.getResponseHeader("Location")); // undefined; 
       }, 

    statusCode: { 
       302: function() { 
           alert("page not found"); 
          } 
       },     
    complete : function(response) { 
       if(response.status == 302) { 
        window.alert('page not found');  
       } 
    } 
     }) 

    }); 

    </script> 

當我看到響應頭,他們顯示爲

Date   Wed, 13 Mar 2013 06:43:18 GMT 
Location  https://testDomain/pageXXX.asp 
Server   Microsoft-IIS/6.0 
Set-Cookie  TravAuthV=e;path=/; domain=testDomain.com; 
X-Powered-By ASP.NET 

我在想什麼..?或者有沒有其他的替代來處理這個

感謝您的時間..任何幫助將不勝感激......

回答

1

你是不是缺少什麼問題在下面一行。

url  : "https://testDomain/login.asp", 

您正在使用AJAX發送SSL請求,我不認爲它的工作,因爲它違反了JavaScript的政策,因爲它沒有看到來自同一來源要求的非SSL鏈接的SSL ..

你能做些什麼?

您可以從服務器添加Access-Control-Allow-Origin頭。

Access-Control-Allow-Origin: https://testDomain/login.asp 

Read More

嘗試使用設置crossDomain : truedataType:jsonp如果沒有jsonp響應然後將其刪除。

$.ajax(
{ 
    url: 'https://testDomain/login.asp', 
    async  : false, 
    type  : 'POST', 
    data  : {Username:'test',Password:'testPass'}, 
    success : function(respText){alert(respText);}, 
    crossDomain: true, 
    dataType: 'jsonp', 
    error: function() { alert('Failed!'); }, 
    beforeSend: function(xhr) 
    { 
     xhr.setRequestHeader('Authorization',getToken()); 
    } 
}); 
+0

你的意思是我不能通過非ssl請求發送ajax請求到ssl域...? – alwaysLearn 2013-03-13 05:58:37

+0

@new_developer看到我的更新答案.. – 2013-03-13 06:07:37

+0

是的,我想它..很快就會更新您 – alwaysLearn 2013-03-13 06:08:29