var formData = new FormData(loginRequestObject);
formData.append('userName', loginRequestObject.username);
formData.append('password', loginRequestObject.password);
formData.append('mobile', loginRequestObject.mobile);
formData.append('deviceid', deviceInfo.getDeviceId())
这就是我的要求-
以及身体部位
try{
const response = yield call(fetch, Url, {
method : 'POST',
headers : {
'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
'Content-Type' :'multipart/form-data, application/x-www-form-urlencoded; charset=utf-8'
},
body : formData
})
if(response.ok) {
const jsonResponse = yield response.text()
yield put(LoginActions.loginSuccess(jsonResponse))
} else {
const jsonResponse = yield response.text();
yield put(LoginActions.loginFailure(jsonResponse))
}
我提出发布数据的请求,但我无法做到,原因是什么?
首先,没有
promise function
处理您的 POST
请求的响应。这段实现的代码出现了什么错误?当您不分多个部分发送数据时,为什么要使用 multipart/form-data
?从您的代码来看,您似乎没有发送所需的图像数据multipart
。
您的问题没有提供足够的详细信息来提供任何具体答案,请用有关您收到的错误的更多详细信息进行解释。
我认为你应该用另一个函数包装 fetch 函数,这是给你的演示
const fetchFunc = ({ url, options }) => {
return fetch(url, options);
}
const response = yield call(fetchFunc, {
url: Url,
options {
method : 'POST',
headers : {
'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
'Content-Type' :'multipart/form-data, application/x-www-form-urlencoded;charset=utf-8',
},
body : formData
}
});
if(response.ok) {
const jsonResponse = yield response.text()
yield put(LoginActions.loginSuccess(jsonResponse))
} else {
const jsonResponse = yield response.text();
yield put(LoginActions.loginFailure(jsonResponse))
}
只是我删除了内容类型,它就起作用了。