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 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献