dongsha1544 2013-03-13 05:49
浏览 1710

使用jquery ajax解决302重定向问题

Due to some requirement I need to login to a site using jquery ajax .. but when I try to do so .. the firebug console is saying that it is 302 error. I have tried to implement the code for its handling but unfortunately with no success

Here is my code I have tried till now :

<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>

when I see the response headers they show up as

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

What am I missing .. ?? Or is there any other alternative to handle this

Thanks for your time .. any help will be appreciated...

  • 写回答

1条回答 默认 最新

  • doumisha5081 2013-03-13 05:56
    关注

    You are not missing anything problem is in below line.

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

    you are sending SSL request using ajax and i don't think its working because it's violates Javascript's policy because it doesn't see the SSL Requested from the same source as the non-SSL url..

    What you can do?

    you can add the Access-Control-Allow-Origin header from the server.

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

    <kbd>Read More</kbd>

    Try using setting crossDomain : true and dataType:jsonp if not jsonp response then remove it.

    $.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());
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试