alphajin 2011-06-11 13:09
浏览 569
已采纳

Google 登录认证!

我需要用JS去实现Google登录。代码如下
Main.prototype.login=function(){
var xhRequest= new XMLHttpRequest(); //getXmlHttpRequestObject();
var url="https://www.google.com/accounts/ClientLogin";
var data="Email="xxxxxxx%40gmail.com&Passwd=xxxxx&service=mail&accountType=HOSTED_OR_GOOGLE";

  xhRequest.onreadystatechange =function(){
    //alert(xhRequest.readyState);
    if (!xhRequest) {
       return;
    }
    if (xhRequest.readyState != 4) {
       return;
    }

    alert(xhRequest.responseText);

    alert("Login successfull");


  }


  xhRequest.open('POST', url, true);
  xhRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xhRequest.send(data);

}
可是我每次得到的responseText都是空的。

着急,请大家帮帮我。谢谢咯!

  • 写回答

2条回答 默认 最新

  • qianzhi008 2011-06-14 09:25
    关注

    [quote]多了一个引号吧?[/quote]
    如果你的浏览器是IE,并且他的版本是IE7(包含IE7)一下的话,你的xhRequest不能这么获取,而是应该用[code="java"]xhRequest = new ActiveXObject("Microsoft.XMLHTTP");[/code]来获取。
    一下是我稍微修改了一下你的代码,是因为我觉得你判断那边有点乱。
    [code="java"]


    修改对象原型
    <p>function re(){<br> alert(&quot;comein&quot;);<br> var xhRequest= new XMLHttpRequest(); <br> var url=&quot;<a href="https://www.google.com/accounts/ClientLogin">https://www.google.com/accounts/ClientLogin</a>&quot;;<br> var data=&quot;Email=******@gmail.com&amp;Passwd=******&amp;service=mail&amp;accountType=HOSTED_OR_GOOGLE&quot;;</p> <pre><code> xhRequest.onreadystatechange =function(){ alert(xhRequest.readyState); if(!xhRequest) { alert(&quot;xmlHttp not supported!&quot;) return; } if(xhRequest.readyState == 4) {// 请求应经完成,这边可能要等一会儿,因为这是一个请求的过程,xhRequest.readyState会变化,当他请求应经完成时xhRequest.readyState就等于4,你就可以取到响应结果了 if(xhRequest.status == 200){// 请求成功 alert(&quot;Login successfull&quot;); alert(xhRequest.responseText); } } } xhRequest.open(&#39;POST&#39;, url, true); xhRequest.setRequestHeader(&quot;Content-Type&quot;,&quot;application/x-www-form-urlencoded&quot;); xhRequest.send(data); </code></pre> <p>} <br>




    [/code]
    希望对你有帮助 :D
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计