1
我試圖擊中1 API,使用多部分數據發出HTTP POST請求,但我得到了400錯誤請求錯誤。400錯誤請求錯誤與HTTP多部分請求
var reqPayload='{
"param1":"value1"
}';
var ajaxOptions = {
url: href,
type: method_NAME,
async: true
};
ajaxOptions.data = new FormData();
ajaxOptions.data.append("jsonInputParameters", $.parseJSON(reqPayload));
ajaxOptions.contentType = false;
ajaxOptions.processData = false;
//following will not create any problem since there is only 1 file.
$.each($('#id_NAME input[type="file"]')[0].files, function(i, file) {
ajaxOptions.data.append('primaryFile', file);
});
我的要求是這樣的:
------WebKitFormBoundary9hoSobTAHgGksFST
Content-Disposition: form-data; name="jsonInputParameters"
[object Object]
Content-Disposition: form-data; name="primaryFile"; filename="example.txt"
Content-Type: text/plain
------WebKitFormBoundary9hoSobTAHgGksFST
但是,我想是這樣的:
------WebKitFormBoundary9hoSobTAHgGksFST
Content-Disposition: form-data; name="jsonInputParameters"
{
"parentID":"FB4CD874EF94CD2CC1B60B72T0000000000100000001"
}
------WebKitFormBoundary9hoSobTAHgGksFST
Content-Disposition: form-data; name="primaryFile"; filename="example.txt"
Content-Type: text/plain
<File Content>
------WebKitFormBoundary9hoSobTAHgGksFST--
請讓我知道了一些變通發出此問題。
但是,變量reqPayload已經是字符串格式。那麼,這有什麼用呢? – Vishwanath
另外,我需要發送對象而不是字符串 – Vishwanath
好點。在這種情況下,只需將其直接附加到FormData而無需任何編碼。 –