Reactjs 请求可与Postman一起使用,但不能与axios.post一起使用
我使用axios.post()在React js中创建了一个注册表。 通过邮递员,请求可以完美地工作,这使得后端没有内疚感, 但我的成分没有任何反应 使用Try/catch或finally()没有任何效果。 控制台中没有任何错误。 我的所有变量定义如下:Reactjs 请求可与Postman一起使用,但不能与axios.post一起使用,reactjs,post,request,axios,postman,Reactjs,Post,Request,Axios,Postman,我使用axios.post()在React js中创建了一个注册表。 通过邮递员,请求可以完美地工作,这使得后端没有内疚感, 但我的成分没有任何反应 使用Try/catch或finally()没有任何效果。 控制台中没有任何错误。 我的所有变量定义如下: constructor(props) { super(props); this.state = { passwd: '' } } onFieldChange(fieldName) { retur
constructor(props) {
super(props);
this.state = {
passwd: ''
}
}
onFieldChange(fieldName) {
return function (event) {
this.setState({[fieldName]: event.target.value});
}
}
render() {
return (
[...]
<form onSubmit={() => this.handleSubmit()}>
<input type="password" value={this.state.passwd}
onChange={this.onFieldChange('passwd').bind(this)}
placeholder="* Password" required />
<button type="submit">Sign-up</button>
</form>
[...]
我期待着收到您的来信。因为您没有给出正确的错误日志,所以无法得到确切的问题。因此,为您提供了我的工作代码之一。这可能会有所帮助。 此代码段获取url,然后发布数据。在这里,您可能会遇到CORS问题。如果是,则在package.json中写入- “代理”:” 并安装http中间件代理。它可以很好地工作:
export function PostData(userData) {
var targetUrl = '/report' /* /register in your case.*/
return new Promise((resolve, reject) => {
fetch(targetUrl, {
method: 'POST',
headers: {
'Content-Type': "application/json; charset=utf-8",
"Accept": "application/json"
},
body: JSON.stringify({
"requestData": {
"userName": userData.username,
"password": userData.password,
"clientId": "ptm",
"txnType": "GDR"
}
})
})
.then(response => response.json())
.then((responseJson) => {
resolve(responseJson)
})
.catch((error) => {
reject(error)
})
})
对于axios one,这是您可以尝试的示例之一:
const user = {
name: this.state.name
};
axios.post(`https://jsonplaceholder.typicode.com/users`, {
user
})
.then(res => {
console.log(res);
console.log(res.data);
})
}
由于您没有提供正确的错误日志,因此无法获得确切的问题。因此,为您提供我的工作代码之一。这可能会有所帮助。 此代码段获取url,然后发布数据。在这里,您可能会遇到CORS问题。如果是,则在package.json中写入- “代理”:” 并安装http中间件代理。它可以很好地工作:
export function PostData(userData) {
var targetUrl = '/report' /* /register in your case.*/
return new Promise((resolve, reject) => {
fetch(targetUrl, {
method: 'POST',
headers: {
'Content-Type': "application/json; charset=utf-8",
"Accept": "application/json"
},
body: JSON.stringify({
"requestData": {
"userName": userData.username,
"password": userData.password,
"clientId": "ptm",
"txnType": "GDR"
}
})
})
.then(response => response.json())
.then((responseJson) => {
resolve(responseJson)
})
.catch((error) => {
reject(error)
})
})
对于axios one,这是您可以尝试的示例之一:
const user = {
name: this.state.name
};
axios.post(`https://jsonplaceholder.typicode.com/users`, {
user
})
.then(res => {
console.log(res);
console.log(res.data);
})
}
在设置标题时,请检查空格、逗号、引号,尤其是空格,而在“邮递员”中,您将其写入框中,但在“代码编辑器”中硬编码时,有时会拼写错误或添加额外的空格vs,这可能会导致错误在设置标题时,请检查空格、逗号、引号,尤其是空格,在《邮递员》中,你把它写在盒子里,但当你在代码编辑器中硬编码时,有时你拼错了或加了额外的空格vs,这可能会导致错误我终于自己找到了解决办法。 “form”标记在发送请求后重新加载页面。
将表单更改为div标签解决了我的问题。我终于自己找到了解决方案。 “form”标记在发送请求后重新加载页面。
将表单更改为div标记解决了我的问题。我遇到了类似的问题,显然我的登录函数接受了2个字符串,但我传递了一个包含2个字符串的json对象,我必须在API控制器中进行一些更改 创建了一个登录类
public class UserLogin
{
public string email { get; set; }
public string pass { get; set; }
}
并为UserLogin创建一个对象作为参数,以便它可以接受userID和密码
public String Post(UserLogin ul){}
我有一个类似的问题,显然我的登录函数接受了2个字符串,但我传递了一个包含2个字符串的json对象,我必须在API控制器中进行一些更改 创建了一个登录类
public class UserLogin
{
public string email { get; set; }
public string pass { get; set; }
}
并为UserLogin创建一个对象作为参数,以便它可以接受userID和密码
public String Post(UserLogin ul){}
浏览器中显示了什么错误?或者url已连接,但响应是emoty?实际上根本没有错误。这就是我被卡住的原因。它在浏览器中显示了什么错误?或者url已连接,但响应是emoty?实际上根本没有错误。这就是我被卡住的原因。另外,试着安慰你的回答,你要检查回答是否在那里,或者它是否捕捉到了任何错误。如果出现任何歧义,请尽可能使用console.log。此外,尝试对您得到的响应进行控制台操作,以检查响应是否存在或是否捕获到任何错误。如果出现任何歧义,请尽可能使用console.log。出于语义考虑,您仍然可以将表单保留在标记中,但在提交时,请调用方法
event.preventDefault()
,以防止表单重新加载页面。出于语义考虑,您仍然可以将表单保留在标记中,但是在提交时,调用方法event.preventDefault()
,以防止表单重新加载页面。