http jsonp support added

This commit is contained in:
Vladimir Enchev
2015-09-07 10:52:49 +03:00
parent ddd65832c0
commit 4cf6b66939
5 changed files with 55 additions and 2 deletions

View File

@ -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 () {
TKUnit.assert(typeof (http.getImage) !== "undefined", "Method http.getImage() should be defined!");
};

View File

@ -4,6 +4,7 @@
import imageSource = require("image-source");
import types = require("utils/types");
import utils = require("utils/utils");
// this is imported for definition purposes only
import http = require("http");
@ -45,7 +46,7 @@ function onRequestComplete(requestId: number, result: com.tns.Async.Http.Request
content: {
raw: result.raw,
toString: () => { return result.responseAsString; },
toJSON: () => { return JSON.parse(result.responseAsString); },
toJSON: () => { return utils.parseJSON(result.responseAsString); },
toImage: () => {
return new Promise<imageSource.ImageSource>((resolveImage, rejectImage) => {
if (result.responseAsImage != null) {

View File

@ -5,6 +5,7 @@
import http = require("http");
import imageSource = require("image-source");
import types = require("utils/types");
import utils = require("utils/utils");
var GET = "GET";
var USER_AGENT_HEADER = "User-Agent";
@ -60,7 +61,9 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
content: {
raw: data,
toString: () => { return NSDataToString(data); },
toJSON: () => { return JSON.parse(NSDataToString(data)); },
toJSON: () => {
return utils.parseJSON(NSDataToString(data));
},
toImage: () => {
return new Promise<imageSource.ImageSource>((resolveImage, rejectImage) => {
var img = imageSource.fromData(data);

View File

@ -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 {
var MODE_SHIFT = 30;
@ -39,6 +48,7 @@ export module layout {
return "Exact";
case layout.AT_MOST:
return "AtMost";
default:

6
utils/utils.d.ts vendored
View File

@ -167,4 +167,10 @@
* @param uri The URI.
*/
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
}