mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 12:57:42 +08:00
Support onload/onerror events on XMLHttpRequest.
This commit is contained in:
@ -598,3 +598,21 @@ export var test_XMLHttpRequest_requestShouldBePossibleAfterAbort = function (don
|
|||||||
|
|
||||||
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
|
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function test_raises_onload_Event(done) {
|
||||||
|
let xhr = new XMLHttpRequest();
|
||||||
|
xhr.onload = () => {
|
||||||
|
done(null);
|
||||||
|
}
|
||||||
|
xhr.open("GET", "https://httpbin.org/get");
|
||||||
|
xhr.send();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function test_raises_onerror_Event(done) {
|
||||||
|
let xhr = new XMLHttpRequest();
|
||||||
|
xhr.onerror = () => {
|
||||||
|
done(null);
|
||||||
|
}
|
||||||
|
xhr.open("GET", "https://no-such-domain-httpbin.org");
|
||||||
|
xhr.send();
|
||||||
|
}
|
||||||
|
3
declarations.d.ts
vendored
3
declarations.d.ts
vendored
@ -38,6 +38,9 @@ declare class XMLHttpRequest {
|
|||||||
readyState: number;
|
readyState: number;
|
||||||
responseText: string;
|
responseText: string;
|
||||||
status: number;
|
status: number;
|
||||||
|
|
||||||
|
onload: () => void;
|
||||||
|
onerror: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
13
http/http.ts
13
http/http.ts
@ -39,6 +39,9 @@ export class XMLHttpRequest {
|
|||||||
public LOADING = 3;
|
public LOADING = 3;
|
||||||
public DONE = 4;
|
public DONE = 4;
|
||||||
|
|
||||||
|
public onload: () => void;
|
||||||
|
public onerror: () => void;
|
||||||
|
|
||||||
private _options: definition.HttpRequestOptions;
|
private _options: definition.HttpRequestOptions;
|
||||||
private _readyState: number;
|
private _readyState: number;
|
||||||
private _status: number;
|
private _status: number;
|
||||||
@ -113,6 +116,7 @@ export class XMLHttpRequest {
|
|||||||
|
|
||||||
}).catch(e => {
|
}).catch(e => {
|
||||||
this._errorFlag = true;
|
this._errorFlag = true;
|
||||||
|
this._setReadyState(this.DONE);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -167,6 +171,15 @@ export class XMLHttpRequest {
|
|||||||
this.onreadystatechange();
|
this.onreadystatechange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this._readyState === this.DONE) {
|
||||||
|
if (this._errorFlag && types.isFunction(this.onerror)) {
|
||||||
|
this.onerror();
|
||||||
|
}
|
||||||
|
if (!this._errorFlag && types.isFunction(this.onload)) {
|
||||||
|
this.onload();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get responseText(): string {
|
get responseText(): string {
|
||||||
|
Reference in New Issue
Block a user