我正在开发一个反应应用程序,所以我有一个内置的webpack。这是我的核心部分与问题相关
import $ from 'jquery'
let ajax = (function () {
const baseUrl = 'http://localhost:8080';
let request = {
get: (url, data) => {
let settings = adjustSettings('GET', url, data);
return $.ajax(settings)
},
put: (url, data) => {
//TODO
},
post: (url, data) => {
let settings = adjustSettings('POST', url, data);
return $.ajax(settings);
},
delete: () => {
}
};
function adjustSettings(method, url, data) {
let settings = {
method: method,
url: baseUrl + url,
data: JSON.stringify(data) || null,
crossDomain: true,
contentType: "application/json",
success: (response, textStatus, xhr) => {
console.log(xhr);
let obj = {
response: response,
xhr: xhr
};
return obj;
},
error: (error) => {
return error;
}
};
return settings;
}
return {request: request}
})();
export default ajax;
我正在导入ajax的另一个文件
import ajax from '../confing/ajax'
ajax.request.post("/user/register", data).then((obj) => {
console.log(obj);
})
现在ajax正在发送到我的后端,它按预期工作,但当我的succes
函数返回obj
时,我似乎无法获取.then((obj)
内的任何数据,如果尝试在其console.log(obj)
函数内success
,数据就在那里,所以tl; dr;我好像无法从我的success
函数中正确返回obj。有什么想法吗?