【发布时间】:2017-02-15 10:56:07
【问题描述】:
我在这里的第一篇文章。 我正在使用 droidscript,我必须包含一个包含特定用户和密码的标头才能检索令牌。我遇到了麻烦,因为我不知道在哪里包含这些标题。
这是我正在使用的代码:
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”);
【问题讨论】:
-
在
httpRequest.open("GET", url, true);inSendRequest( url )之前尝试一下 -
没用。它给了我这个错误:脚本错误:无法在“XMLHttpRequest”上执行“setRequestHeader”:必须打开对象的状态。
-
好吧,正如它所说的“对象的状态必须是打开的”,显然应该在发送请求之前设置标头,那么如何将代码放在
open()之后和send()之前。 -
那么它必须在
httpRequest.open("GET", url, true);之后但在httpRequest.send(null);之前 -
检索此错误:错误 0
标签: javascript android rest http get