Commit 31be7dbd authored by haiyoucuv's avatar haiyoucuv

整理一下代码1

parent 3c07a5e8
/** /**
* 到时放到Loader里,增加open类型、headers、参数、等等 * 到时放到Loader里,增加open类型、headers、参数、等等
* @param options * @param options
*/ */
export function ajax(options: ajaxParameterInt) { export function ajax(options: ajaxParameterInt) {
/** /**
...@@ -18,7 +18,6 @@ export function ajax(options: ajaxParameterInt) { ...@@ -18,7 +18,6 @@ export function ajax(options: ajaxParameterInt) {
/** /**
* 对需要传入的参数的处理 * 对需要传入的参数的处理
*/ */
var params = getParams(options.data);
var xhr: XMLHttpRequest; var xhr: XMLHttpRequest;
/** /**
* 创建一个 ajax请求 * 创建一个 ajax请求
...@@ -60,6 +59,7 @@ export function ajax(options: ajaxParameterInt) { ...@@ -60,6 +59,7 @@ export function ajax(options: ajaxParameterInt) {
// }; // };
if (options.type == 'GET') { if (options.type == 'GET') {
const params = getParams(options.data);
xhr.open("GET", options.url + '?' + params, options.async); xhr.open("GET", options.url + '?' + params, options.async);
//get请求也会需要设置请求头的情况 //get请求也会需要设置请求头的情况
if (options.headers) { if (options.headers) {
...@@ -83,8 +83,9 @@ export function ajax(options: ajaxParameterInt) { ...@@ -83,8 +83,9 @@ export function ajax(options: ajaxParameterInt) {
} }
} }
if(options.headers["Content-Type"] == "application/json"){ let params = getParams(options.data);
params = JSON.stringify(params); if (options.headers["Content-Type"] == "application/json") {
params = JSON.stringify(options.data);
} }
/** /**
...@@ -96,8 +97,8 @@ export function ajax(options: ajaxParameterInt) { ...@@ -96,8 +97,8 @@ export function ajax(options: ajaxParameterInt) {
/** /**
* jsonp模拟,不考虑回调 * jsonp模拟,不考虑回调
* @param url * @param url
* @param params * @param params
*/ */
export function jsonp(url: string, params: any) { export function jsonp(url: string, params: any) {
const src = url + '?' + getParams(params); const src = url + '?' + getParams(params);
...@@ -128,6 +129,7 @@ function getParams(data): string { ...@@ -128,6 +129,7 @@ function getParams(data): string {
arr.push('_=' + Date.now()); arr.push('_=' + Date.now());
return arr.join('&'); return arr.join('&');
} }
//基本没用到过cache,先不加 //基本没用到过cache,先不加
interface ajaxParameterInt { interface ajaxParameterInt {
url: string, url: string,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment