简体   繁体   English

在 Redux-saga 中使用 Fetch 发布表单数据

[英]Post form data using Fetch in Redux-saga

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())

This is the request What I am making-这是我的要求——

and in body part和身体部位

  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))
}

I am making this request to post the data but I'm not able to do , what is the reason?我正在提出发布数据的请求,但我无法做到,这是什么原因?

First of all, there is no promise function handling the response of your POST request.首先,没有处理POST请求响应的promise function What error are you getting with this implemented code?这个实现的代码有什么错误? Why are you using multipart/form-data when you are not sending data in multiple parts?当您不分多个部分发送数据时,为什么要使用multipart/form-data From your code it doesn't look like you are sending the image data for which you need multipart .从您的代码来看,您似乎没有发送需要multipart的图像数据。

Your question doesn't provide enough details to provide any specific answer, please explain it with more details about the error you are getting.您的问题没有提供足够的细节来提供任何具体的答案,请详细解释您遇到的错误。

I think you should wrap fetch function with another function, this is a demo for you我认为你应该用另一个函数包装 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))
}

只是我删除了 Content-Type 并且它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM