GET 方法可以正常工作,但 POST 总是有问题。我的测试代码是
let server = sinon.createFakeServer();
server.respondImmediately = true;
server.respondWith(`api/v1.0/companies/${companyResponse.id}/orgchart`, [200, {"Content-Type": "*/*"}, 'OK']);
下面是ajax调用:
ajax({
type: "POST",
url: '/my/api/endpoint',
data: data,
processData: false,
contentType: false,
complete: this.handleLoadingComplete,
xhr: function(){
let xhr = $.ajaxSettings.xhr() ;
xhr.upload.onprogress = function(evt){
updateProgress(evt.loaded, evt.total);
} ;
xhr.upload.onload = function(){ console.log('loading completed') } ;
return xhr ;
}
});
使用完全相同的测试代码,当我将 ajax 方法设置为“GET”时,它可以工作。但是对于“POST”,它总是失败。complete
这是上述 ajax POST收到的响应:
`
{ readyState: 0,
getResponseHeader: [Function: getResponseHeader],
getAllResponseHeaders: [Function: getAllResponseHeaders],
setRequestHeader: [Function: setRequestHeader],
overrideMimeType: [Function: overrideMimeType],
statusCode: [Function: statusCode],
abort: [Function: abort],
state: [Function: state],
always: [Function: always],
catch: [Function: catch],
pipe: [Function: pipe],
then: [Function: then],
promise: [Function: promise],
progress: [Function: add],
done: [Function: add],
fail: [Function: add],
status: 0,
statusText:
'TypeError: By RFC7230, section 3.2.4, header values should be strings. Got object' }
`我不知道这里有什么错误,我被困了好几个小时......有人可以帮忙吗?将不胜感激!