我试图在我的react-native中使用post方法进行提取,但是我总是收到一个错误:
TypeError: Network request failed
at XMLHttpRequest.xhr.onerror (whatwg-fetch.js:504)
at XMLHttpRequest.dispatchEvent (event-target.js:172)
at XMLHttpRequest.setReadyState (XMLHttpRequest.js:580)
at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:394)
at XMLHttpRequest.js:507
at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
at MessageQueue.__callFunction (MessageQueue.js:366)
at MessageQueue.js:106
at MessageQueue.__guard (MessageQueue.js:314)
at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:105)
用户创建g页面
static createUser(Identity, FirstName, LastName, FiscalCode , Email) {
let formdata = new FormData();
formdata.append('Identity', JSON.stringify(Identity));
formdata.append('FirstName', (FirstName));
formdata.append('LastName', LastName);
formdata.append('FiscalCode', FiscalCode);
formdata.append('Email', Email);
return new Promise((resolve, reject)=> {
fetch('https://linktomyapi.com' , {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
body: formdata
})
.then((response) => response.json())
.then((responseData) => {
if(responseData.Error){
Alert.alert("Errore");
}
global.utente = responseData;
resolve(responseData)
})
.catch((err) => {reject(err)})
})
}
关于身份,我这样看:
let Identity =
{
AppName:
{
Username: this.state.FiscalCode,
Password: this.state.Password
}
}
我遵循许多有关此参数的指南,但是我不明白为什么会收到这些错误。
答案 0 :(得分:0)
您可以尝试以下吗?
let params = {
Identity: JSON.stringify(Identity),
FirstName: FirstName,
LastName: LastName,
FiscalCode: FiscalCode,
Email: Email
}
return new Promise((resolve, reject)=> {
fetch('https://linktomyapi.com' , {
method: 'POST',
headers: {
"Content-Type": "application/json",
},
...(params && { body: JSON.stringify(params) })
})
.then((response) => response.json())
.then((responseData) => {
if(responseData.Error){
Alert.alert("Errore");
}
global.utente = responseData;
resolve(responseData)
})
.catch((err) => {reject(err)})
})
确定您的内容类型是formdata?
答案 1 :(得分:0)
问题在那里:
return new Promise((resolve, reject)=> {
fetch('https://linktomyapi.com' , {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
body: formdata
})
在提取中,我将链接作为字符串传递。
因此,正确的表单是:
return new Promise((resolve, reject)=> {
fetch(https://linktomyapi.com , {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
body: formdata
})