尝试将post方法从react js应用到asp.net时出错400
当我从用户那里获得输入时,我试图实现post方法,但是当我提交结果时。它显示结果是成功的,但它显示了一个400错误的错误请求 reactjs中的post方法:尝试将post方法从react js应用到asp.net时出错400,asp.net,reactjs,post,Asp.net,Reactjs,Post,当我从用户那里获得输入时,我试图实现post方法,但是当我提交结果时。它显示结果是成功的,但它显示了一个400错误的错误请求 reactjs中的post方法: constructor(props){ super(props) this.state = { Order_ID: '', Name: '', Age: 0 } } changeHandler = e => { console.log(&quo
constructor(props){
super(props)
this.state = {
Order_ID: '',
Name: '',
Age: 0
}
}
changeHandler = e => {
console.log("Hello!")
this.setState({ [e.target.name]: e.target.value })
}
submitHandler = e => {
e.preventDefault()
fetch('http://localhost:54507/api/customers',{
method:'post',
mode:'cors',
headers:{
'Accept':'application/json',
'Content-Type':'application/json'
},
body:JSON.stringify({
Order_ID:e.target.Order_ID.value,
Name:e.target.Name.value,
Age:parseInt(e.target.Age.value,10)
})
})
.then(res=> res.json())
.then((result)=>{
alert('Success')
},
(error)=>{
alert('Failed')
}
)
}
visual studio中的我的post方法:
public HttpResponseMessage Post([FromBody] Customer customer)
{
try
{
using (PharamcyEntities entities = new PharamcyEntities())
{
entities.Customers.Add(customer);
entities.SaveChanges();
var message = Request.CreateResponse(HttpStatusCode.Created, customer);
message.Headers.Location = new Uri(Request.RequestUri + customer.Order_ID.ToString());
return message;
}
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
注意:我使用[启用CORS(“*”、“*”、“*”)]
我能够从VisualStudio中将问题跟踪到实体;
请帮忙(