diff --git a/LINQ.ts/Framework/Define/Abstracts/TS.ts b/LINQ.ts/Framework/Define/Abstracts/TS.ts index 0202917..edc1f15 100644 --- a/LINQ.ts/Framework/Define/Abstracts/TS.ts +++ b/LINQ.ts/Framework/Define/Abstracts/TS.ts @@ -161,11 +161,15 @@ * 请尽量使用upload方法进行文件的上传 * * @param url 目标数据源,这个参数也支持meta标签的查询语法 + * @param options + * 1. sendContentType + * 2. wrapPlantTextError 当后台正常返回一段存文本(但是无法被解析为json),是否封装为一个错误消息给调用函数? */ post(url: string, data: object | FormData, callback?: ((response: IMsg) => void), options?: { - sendContentType?: boolean + sendContentType?: boolean, + wrapPlantTextError?: boolean }): void; /** * 请注意:这个函数只会接受来自后端的json返回,如果不是json格式,则可能会解析出错 diff --git a/LINQ.ts/Framework/Define/Internal.ts b/LINQ.ts/Framework/Define/Internal.ts index 0ae31ef..6f48ae3 100644 --- a/LINQ.ts/Framework/Define/Internal.ts +++ b/LINQ.ts/Framework/Define/Internal.ts @@ -48,20 +48,22 @@ namespace Internal { ts.post = function (url: string, data: object | FormData, callback?: ((response: IMsg<{}>) => void), options?: { - sendContentType?: boolean + sendContentType?: boolean, + wrapPlantTextError?: boolean }) { + var opts = options ?? {}; var contentType: string = HttpHelpers.measureContentType(data); var post = { type: contentType, data: data, - sendContentType: (options || {}).sendContentType || true + sendContentType: opts.sendContentType || true }; HttpHelpers.POST(urlSolver(url), post, function (response, code) { if (callback) { if (code == 200) { - callback(handleJSON(response)); + callback(handleJSON(response, opts.wrapPlantTextError ?? false)); } else { // handle page not found and internal server error callback(>{ @@ -92,7 +94,7 @@ namespace Internal { HttpHelpers.GetAsyn(urlSolver(url), function (response, code: number) { if (callback) { if (code == 200) { - callback(handleJSON(response)); + callback(handleJSON(response, true)); } else { // handle page not found and internal server error callback(>{ @@ -107,7 +109,7 @@ namespace Internal { ts.upload = function (url: string, file: File, callback?: ((response: IMsg<{}>) => void)) { HttpHelpers.UploadFile(urlSolver(url), file, null, function (response) { if (callback) { - callback(handleJSON(response)); + callback(handleJSON(response, true)); } }); }; @@ -223,7 +225,7 @@ namespace Internal { return url; } - function handleJSON(response: any): any { + function handleJSON(response: any, wrapPlantTextError: boolean): any { if (typeof response == "string") { /* @@ -236,10 +238,17 @@ namespace Internal { try { return JSON.parse(response); } catch (ex) { - console.error("Invalid json text: "); - console.error(response); + if (wrapPlantTextError) { + return >{ + code: 500, + info: response + } + } else { + console.error("Invalid json text: "); + console.error(response); - throw ex; + throw ex; + } } } else { return response;