为什么在使用 Redux Store 的 Fetch 请求中未定义响应
Why is response undefined in Fetch request using Redux Store
我正在向应用程序的 post 新用户写一个 Fetch 请求。 fetch 与 redux store 集成在一起。响应 returns [object Object] 和 response.status returns 未定义。我是 Redux 的新手,想知道这是否是错误所在。这是我的动作创建者文件中的代码:
export function createCustomerSuccess(values) {
return {
type: types.CREATE_CUSTOMER_SUCCESS,
values: values
};
}
export function createCustomer(values) {
return function (dispatch, getState) {
console.log('values passing to store', values);
return postIndividual(values).then( (response) => {
console.log('calling customer actions');
console.log(response);
if(response.status === 200){
console.log(response.status);
dispatch(createCustomerSuccess(values));
console.log('create customer success');
}
else {
console.log('not successful');
}
});
};
}
function postIndividual(values) {
console.log('test from post' + JSON.stringify(values));
const URLPOST = "http://myurlisworking/Add";
return fetch (URLPOST, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Access-Control-Origin": "*"
},
body: JSON.stringify(values)
})
.then(response => response.json())
.then(response => {
console.log('response' + response.status)
});
}
问题似乎与您的提取期望有关。当你的第一个 .then
在 fetch()
之后被调用时,你会得到 response.status
可以在那里检查。
您可以像下面这样重写您的抓取,看看是否能解决问题。
function postIndividual(values) {
console.log('test from post' + JSON.stringify(values));
const URLPOST = "http://myurlisworking/Add";
return fetch (URLPOST, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Access-Control-Origin": "*"
},
body: JSON.stringify(values)
})
.then(response => {
console.log('response' + response.status)
return response.ok && response.json();
})
.catch(err => console.log('Error:', err));
}
您可以在此处查看 response.status
^ 并执行您想要的操作。
或者您可以只在 postIndividual
中执行 fetch
并在 createCustomer
中处理响应。
我正在向应用程序的 post 新用户写一个 Fetch 请求。 fetch 与 redux store 集成在一起。响应 returns [object Object] 和 response.status returns 未定义。我是 Redux 的新手,想知道这是否是错误所在。这是我的动作创建者文件中的代码:
export function createCustomerSuccess(values) {
return {
type: types.CREATE_CUSTOMER_SUCCESS,
values: values
};
}
export function createCustomer(values) {
return function (dispatch, getState) {
console.log('values passing to store', values);
return postIndividual(values).then( (response) => {
console.log('calling customer actions');
console.log(response);
if(response.status === 200){
console.log(response.status);
dispatch(createCustomerSuccess(values));
console.log('create customer success');
}
else {
console.log('not successful');
}
});
};
}
function postIndividual(values) {
console.log('test from post' + JSON.stringify(values));
const URLPOST = "http://myurlisworking/Add";
return fetch (URLPOST, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Access-Control-Origin": "*"
},
body: JSON.stringify(values)
})
.then(response => response.json())
.then(response => {
console.log('response' + response.status)
});
}
问题似乎与您的提取期望有关。当你的第一个 .then
在 fetch()
之后被调用时,你会得到 response.status
可以在那里检查。
您可以像下面这样重写您的抓取,看看是否能解决问题。
function postIndividual(values) {
console.log('test from post' + JSON.stringify(values));
const URLPOST = "http://myurlisworking/Add";
return fetch (URLPOST, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Access-Control-Origin": "*"
},
body: JSON.stringify(values)
})
.then(response => {
console.log('response' + response.status)
return response.ok && response.json();
})
.catch(err => console.log('Error:', err));
}
您可以在此处查看 response.status
^ 并执行您想要的操作。
或者您可以只在 postIndividual
中执行 fetch
并在 createCustomer
中处理响应。