mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 04:41:36 +08:00
http jsonp support added
This commit is contained in:
@ -130,6 +130,39 @@ export var test_getJSON_fail_when_result_is_not_JSON = function (done) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export var test_getJSONP = function (done) {
|
||||||
|
var result;
|
||||||
|
|
||||||
|
http.getJSON("http://demos.telerik.com/kendo-ui/service/Products").then(function (r) {
|
||||||
|
result = r;
|
||||||
|
try {
|
||||||
|
TKUnit.assert(typeof (JSON.stringify(result)) === "string", "Result from getJSON() should be valid JSON object!");
|
||||||
|
done(null);
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
done(e);
|
||||||
|
}
|
||||||
|
done(null);
|
||||||
|
}, function (e) {
|
||||||
|
done(e);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export var test_getJSON_fail_when_result_is_not_JSONP = function (done) {
|
||||||
|
var result;
|
||||||
|
|
||||||
|
http.getJSON({ url: "https://httpbin.org/html", method: "GET" }).catch(function (e) {
|
||||||
|
result = e;
|
||||||
|
try {
|
||||||
|
TKUnit.assert(result instanceof Error, "Result from getJSON().catch() should be Error! Current type is " + typeof result);
|
||||||
|
done(null);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export var test_getImage_isDefined = function () {
|
export var test_getImage_isDefined = function () {
|
||||||
TKUnit.assert(typeof (http.getImage) !== "undefined", "Method http.getImage() should be defined!");
|
TKUnit.assert(typeof (http.getImage) !== "undefined", "Method http.getImage() should be defined!");
|
||||||
};
|
};
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
import imageSource = require("image-source");
|
import imageSource = require("image-source");
|
||||||
import types = require("utils/types");
|
import types = require("utils/types");
|
||||||
|
import utils = require("utils/utils");
|
||||||
|
|
||||||
// this is imported for definition purposes only
|
// this is imported for definition purposes only
|
||||||
import http = require("http");
|
import http = require("http");
|
||||||
@ -45,7 +46,7 @@ function onRequestComplete(requestId: number, result: com.tns.Async.Http.Request
|
|||||||
content: {
|
content: {
|
||||||
raw: result.raw,
|
raw: result.raw,
|
||||||
toString: () => { return result.responseAsString; },
|
toString: () => { return result.responseAsString; },
|
||||||
toJSON: () => { return JSON.parse(result.responseAsString); },
|
toJSON: () => { return utils.parseJSON(result.responseAsString); },
|
||||||
toImage: () => {
|
toImage: () => {
|
||||||
return new Promise<imageSource.ImageSource>((resolveImage, rejectImage) => {
|
return new Promise<imageSource.ImageSource>((resolveImage, rejectImage) => {
|
||||||
if (result.responseAsImage != null) {
|
if (result.responseAsImage != null) {
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
import http = require("http");
|
import http = require("http");
|
||||||
import imageSource = require("image-source");
|
import imageSource = require("image-source");
|
||||||
import types = require("utils/types");
|
import types = require("utils/types");
|
||||||
|
import utils = require("utils/utils");
|
||||||
|
|
||||||
var GET = "GET";
|
var GET = "GET";
|
||||||
var USER_AGENT_HEADER = "User-Agent";
|
var USER_AGENT_HEADER = "User-Agent";
|
||||||
@ -60,7 +61,9 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
|
|||||||
content: {
|
content: {
|
||||||
raw: data,
|
raw: data,
|
||||||
toString: () => { return NSDataToString(data); },
|
toString: () => { return NSDataToString(data); },
|
||||||
toJSON: () => { return JSON.parse(NSDataToString(data)); },
|
toJSON: () => {
|
||||||
|
return utils.parseJSON(NSDataToString(data));
|
||||||
|
},
|
||||||
toImage: () => {
|
toImage: () => {
|
||||||
return new Promise<imageSource.ImageSource>((resolveImage, rejectImage) => {
|
return new Promise<imageSource.ImageSource>((resolveImage, rejectImage) => {
|
||||||
var img = imageSource.fromData(data);
|
var img = imageSource.fromData(data);
|
||||||
|
@ -20,6 +20,15 @@ export function copyFrom(source: any, target: any) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function parseJSON(source: string): any {
|
||||||
|
var src = source.trim();
|
||||||
|
if (src.lastIndexOf(")") === src.length - 1) {
|
||||||
|
return JSON.parse(src.substring(src.indexOf("(") + 1, src.lastIndexOf(")")));
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSON.parse(src);
|
||||||
|
}
|
||||||
|
|
||||||
export module layout {
|
export module layout {
|
||||||
|
|
||||||
var MODE_SHIFT = 30;
|
var MODE_SHIFT = 30;
|
||||||
@ -39,6 +48,7 @@ export module layout {
|
|||||||
return "Exact";
|
return "Exact";
|
||||||
|
|
||||||
case layout.AT_MOST:
|
case layout.AT_MOST:
|
||||||
|
|
||||||
return "AtMost";
|
return "AtMost";
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
6
utils/utils.d.ts
vendored
6
utils/utils.d.ts
vendored
@ -167,4 +167,10 @@
|
|||||||
* @param uri The URI.
|
* @param uri The URI.
|
||||||
*/
|
*/
|
||||||
export function isDataURI(uri: string): boolean
|
export function isDataURI(uri: string): boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns object from JSON or JSONP string.
|
||||||
|
* @param source The JSON or JSONP string.
|
||||||
|
*/
|
||||||
|
export function parseJSON(source: string): any
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user