2016-10-06 4 views
0

내 첫 게시물을 여기에. droidscript를 사용하고 있으며 토큰을 검색하기 위해 특정 사용자와 비밀번호가 포함 된 헤더를 포함해야합니다. 머리말을 어디에 넣어야할지 모르니까 문제가 있습니다. http 요청에 헤더 추가

function btn_OnTouch(){ 

    var url = "myurl"; 
    SendRequest(url); 

} 

//Send an http get request. 

function SendRequest(url){ 

    var httpRequest = new XMLHttpRequest(); 

    httpRequest.onreadystatechange = function() { 
     HandleReply(httpRequest); 
    }; 

    httpRequest.open("GET", url, true); 

    httpRequest.send(null); 

    app.ShowProgress("Loading..."); 

} 

//Handle the servers reply (a json object). 

function HandleReply(httpRequest){ 

    if (httpRequest.readyState == 4){ 

     //If we got a valid response. 

     if (httpRequest.status == 200){ 
      txt.SetText("Response: " + httpRequest.status + httpRequest.responseText); 
     } 

     //An error occurred 
     else 
      txt.SetText("Error: " + httpRequest.status + httpRequest.responseText); 

    } 

    app.HideProgress(); 

} 

이 (가) 나는이 같은 헤더를 아마를 포함해야합니다,하지만 난 내 코드에 넣어 위치를 모르는 나에게 말했다 : 내가 사용 코드입니다

.

httpRequest.setRequestHeader(“username”, “myuser”); 

httpRequest.setRequestHeader(“password”, “mypass”); 
+0

이 httpRequest.open'전에 그것을 시도 ("GET", URL, TRUE);있는 sendRequest'에서'를 (url)' –

+0

작동하지 않았습니다.이 오류는 다음과 같습니다. 스크립트 오류 : "XMLHttpRequest"에서 "setRequestHeader"를 실행하지 못했습니다. 개체의 상태를 열어야합니다. –

+0

글쎄, "객체의 상태가 열려 있어야합니다"라고 말했고 요청을 보내기 전에 헤더를 설정해야합니다. 그런 다음 open()과 send() 전에 코드를 삽입하는 방법은 분명합니다. –

답변

0

당신은 다음과 같은 오픈 메소드를 호출 후 작업을 수행해야합니다 -

httpRequest.open("GET", url, true); 
httpRequest.setRequestHeader("username", "myuser"); 
httpRequest.setRequestHeader("password", "mypass"); 
httpRequest.send(null); 
관련 문제