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

這是我的要求——

和身體部位

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

我正在提出發布數據的請求,但我無法做到,這是什么原因?

首先,沒有處理POST請求響應的promise function 這個實現的代碼有什么錯誤? 當您不分多個部分發送數據時,為什么要使用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))
}

只是我刪除了 Content-Type 並且它起作用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM