mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-20 07:26:11 +08:00
headers are now similar to jquery + readme updated
This commit is contained in:
@ -34,34 +34,31 @@
|
|||||||
http.request({
|
http.request({
|
||||||
url: "http://ip.jsontest.com/",
|
url: "http://ip.jsontest.com/",
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: [{ name: "Content-Type", value: "application/json" }]
|
headers: { "Content-Type" : "application/json" }
|
||||||
}).then(function (r) {
|
}).then(function (r) {
|
||||||
var status = r.statusCode;
|
var status = r.statusCode;
|
||||||
|
|
||||||
for (var i = 0, l = r.headers.length; i < l; i++) {
|
for (var header in r.headers) {
|
||||||
var header = r.headers[i];
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = r.content.toJSON();
|
var result = r.content.toJSON();
|
||||||
|
|
||||||
}).fail(function (e) { });
|
}).fail(function (e) { });
|
||||||
|
|
||||||
// Post request
|
// Post request
|
||||||
http.request({
|
http.request({
|
||||||
url: "http://posttestserver.com/post.php?dump&html&dir=test",
|
url: "http://posttestserver.com/post.php?dump&html&dir=test",
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: [{ name: "Content-Type", value: "application/x-www-form-urlencoded" }],
|
headers: { "Content-Type" : "application/x-www-form-urlencoded" },
|
||||||
content: "MyVariableOne=ValueOne&MyVariableTwo=ValueTwo"
|
content: "MyVariableOne=ValueOne&MyVariableTwo=ValueTwo"
|
||||||
}).then(function (r) {
|
}).then(function (r) {
|
||||||
log("Status code:" + r.statusCode);
|
var status = r.statusCode;
|
||||||
|
|
||||||
for (var i = 0, l = r.headers.length; i < l; i++) {
|
for (var header in r.headers) {
|
||||||
var header = r.headers[i];
|
//
|
||||||
log(header.name + ":" + header.value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log("Content:" + r.content.toString())
|
var result = r.content.toJSON();
|
||||||
|
|
||||||
}).fail(function (e) { log(e) });
|
}).fail(function (e) { log(e) });
|
||||||
|
|
||||||
http.getString("http://www.reddit.com/").then(function(result) {
|
http.getString("http://www.reddit.com/").then(function(result) {
|
||||||
|
@ -11,10 +11,9 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
|
|||||||
try {
|
try {
|
||||||
var headers = new com.koushikdutta.async.http.libcore.RawHeaders();
|
var headers = new com.koushikdutta.async.http.libcore.RawHeaders();
|
||||||
|
|
||||||
if (options.headers && options.headers.length) {
|
if (options.headers) {
|
||||||
for (var i = 0, l = options.headers.length; i < l; i++) {
|
for (var key in options.headers) {
|
||||||
var header = options.headers[i];
|
headers.add(key, options.headers[key])
|
||||||
headers.add(header.name, header.value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +36,7 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
|
|||||||
toImage: () => { return require("Image/image").Image.imageFromNativeBitmap(data); }
|
toImage: () => { return require("Image/image").Image.imageFromNativeBitmap(data); }
|
||||||
},
|
},
|
||||||
statusCode: 0,
|
statusCode: 0,
|
||||||
headers: []
|
headers: {}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
9
net/http_request.d.ts
vendored
9
net/http_request.d.ts
vendored
@ -9,18 +9,13 @@ export declare function request(options: HttpRequestOptions): promises.Promise<H
|
|||||||
export interface HttpRequestOptions {
|
export interface HttpRequestOptions {
|
||||||
url: string;
|
url: string;
|
||||||
method: string;
|
method: string;
|
||||||
headers?: HttpHeader[];
|
headers?: any;
|
||||||
content?: any;
|
content?: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface HttpHeader {
|
|
||||||
name: string;
|
|
||||||
value: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface HttpResponse {
|
export interface HttpResponse {
|
||||||
statusCode: number;
|
statusCode: number;
|
||||||
headers: HttpHeader[];
|
headers: any;
|
||||||
content?: HttpContent;
|
content?: HttpContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,13 +16,13 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
|
|||||||
var urlRequest = Foundation.NSMutableURLRequest.requestWithURL(
|
var urlRequest = Foundation.NSMutableURLRequest.requestWithURL(
|
||||||
Foundation.NSURL.URLWithString(options.url));
|
Foundation.NSURL.URLWithString(options.url));
|
||||||
|
|
||||||
|
if (options.method) {
|
||||||
urlRequest.setHTTPMethod(options.method);
|
urlRequest.setHTTPMethod(options.method);
|
||||||
|
}
|
||||||
|
|
||||||
if (options.headers && options.headers.length) {
|
if (options.headers) {
|
||||||
for (var i = 0, l = options.headers.length; i < l; i++) {
|
for (var header in options.headers) {
|
||||||
var header = options.headers[i];
|
urlRequest.setValueForHTTPHeaderField(options.headers[header], header);
|
||||||
|
|
||||||
urlRequest.setValueForHTTPHeaderField(header.name, header.value);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,14 +36,13 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
|
|||||||
d.reject(new Error(error.localizedDescription()));
|
d.reject(new Error(error.localizedDescription()));
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
var headers = new Array<http.HttpHeader>();
|
var headers = {};
|
||||||
var headerFields = response.allHeaderFields();
|
var headerFields = response.allHeaderFields();
|
||||||
var keys = headerFields.allKeys();
|
var keys = headerFields.allKeys();
|
||||||
|
|
||||||
for (var i = 0, l = keys.count(); i < l; i++) {
|
for (var i = 0, l = keys.count(); i < l; i++) {
|
||||||
var key = keys.objectAtIndex(i);
|
var key = keys.objectAtIndex(i);
|
||||||
|
headers[key] = headerFields.valueForKey(key);
|
||||||
headers.push({ name: key, value: headerFields.valueForKey(key) });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
d.resolve({
|
d.resolve({
|
||||||
|
Reference in New Issue
Block a user