2012-01-31 20 views
0

我有一個JavaScript函數,它需要兩個值作爲參數,我從HTML發送(用戶名和令牌值(例如:34378463782)。我試圖使用GET方法進行休息調用,並且必須將令牌作爲?requestheader與呼叫所有我需要知道的,是有什麼錯碼xhr GET與一些標題不起作用?

這裏是代碼:

   function getUser(user,token) 
       { 
       var xmlhttp; 
       var text,x,i; 
       var url="http://abc.xyz.com:8890/uauth/" +user; 


      alert("url :"+url); 



     if (window.XMLHttpRequest) 

       {// code for IE7+, Firefox, Chrome, Opera, Safari 
       xmlhttp=new XMLHttpRequest(); 

        if (xmlhttp==null) 
         { 

         alert ("Browser does not support HTTP Request"); 
         return; 

         } 
       } 

     else 

       {// code for IE6, IE5 

       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 

       } 

     xmlhttp.open("GET",url, true); 
     alert("Outside the Main Code"); 
     xmlhttp.onreadystatechange=function() 
       { 
         alert("Above the Main Code if block"); 
         if (xmlhttp.readyState==4 && xmlhttp.status==200) 
          { 
          alert("Inside the Main Code"); 

          xmlDoc=xmlhttp.responseXML; 
          text=""; 
          x=xmlDoc.getElementsByTagName("response"); 

           for (i=0;i<x.length;i++) 
            { 
            text=text + x[i].childNodes[0].nodeValue + "<br />"; 
            } 

          document.getElementById("myNewDiv").innerHTML=text; 

          } 

        }; 
     xmlhttp.setRequestHeader("xyz-Authorization: ", token); 
     xmlhttp.send(null); 


    } 

PLS jQuery的使用不建議

歡迎評論

我在這裏先向您的幫助表示感謝。

回答

0

我建議使用jquery來xhr-ing。這更清楚。