我是react-native
世界的新手。我使用以下代码从服务器获取响应。虽然调试它工作正常,但没有调试它会给出以下错误
fetch("http://hcdsny.trantorinc.com/index.php/api/register", {
method: "POST",
headers: 'application/x-www-form-urlencoded',
body: JSON.stringify(data)
})
.then(function(response){
return response.json();
})
.then(function(data){
}).catch((error) => {
console.error(error);
});
提前致谢!
答案 0 :(得分:0)
标题应该是一个对象,而不是一个字符串。您要查找的标题的关键字是content-type
:
headers: {
'content-type': 'application/x-www-form-urlencoded'
}
答案 1 :(得分:0)
我遇到了同样的问题,并通过将标题替换为对象而非字符串来解决此问题。
试试这个。
替换(标题为字符串)
headers: 'application/x-www-form-urlencoded'
with(Header as a Object)
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
它可能适合你。