Reactjs 如何从react Js执行WEBAPI Post调用?
这个问题已经被问到了。但我还是不清楚。 我必须在reactjs中调用WEBAPI的POST方法。但我已经在webapi中创建了这个模型。所以我想知道如何将模型数据传递给reactjs中的post调用 型号:Reactjs 如何从react Js执行WEBAPI Post调用?,reactjs,asp.net-web-api,Reactjs,Asp.net Web Api,这个问题已经被问到了。但我还是不清楚。 我必须在reactjs中调用WEBAPI的POST方法。但我已经在webapi中创建了这个模型。所以我想知道如何将模型数据传递给reactjs中的post调用 型号: public class EmployeeModels { public int Id { get; set; } public string name { get; set; } public string mobile { get; set; } publ
public class EmployeeModels
{
public int Id { get; set; }
public string name { get; set; }
public string mobile { get; set; }
public string email { get; set; }
public string dept { get; set; }
public string erole { get; set; }
}
我的WEBAPI发布方法:
//Insert new Employee
public IHttpActionResult CreateNewEmployee(EmployeeModels emp)
{
using (var ctx = new Employee())
{
ctx.tempemp.Add(new tempemp()
{
Id = emp.Id,
name = emp.name,
mobile = emp.mobile,
erole = emp.erole,
dept = emp.dept,
email = emp.email
});
ctx.SaveChanges();
}
return Ok();
}
现在我想发布reactjs中的Employeemodel。请给出任何建议 我已经给出了答案:
let employee={
Id:1,
name:'abc',
mobile:123456,
email:'abc@abc.com',
dept:'IT',
role:'Developer'
}
fetch('https://mywebsite.com/CreateNewEmployee/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(employee)
})
.then(function(resp){
// your response
})
我已经回答了:
let employee={
Id:1,
name:'abc',
mobile:123456,
email:'abc@abc.com',
dept:'IT',
role:'Developer'
}
fetch('https://mywebsite.com/CreateNewEmployee/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(employee)
})
.then(function(resp){
// your response
})
您可以使用axios库
npm install axios --save
然后:
axios.post('/CreateNewEmployee/', {
Id:1,
name:'abc',
mobile:123456,
email:'abc@abc.com',
dept:'IT',
role:'Developer'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
您可以使用axios库
npm install axios --save
然后:
axios.post('/CreateNewEmployee/', {
Id:1,
name:'abc',
mobile:123456,
email:'abc@abc.com',
dept:'IT',
role:'Developer'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
伟大的但我想从用户输入中获取数据。不是硬编码的。你能举个例子吗?
fetch
不是React关键字。这是一个Javascript API。类似于XHR。看看这个@PiyushDhamecha没问题@Karthikeyan也可以查看这个示例代码:很好。但我想从用户输入中获取数据。不是硬编码的。你能举个例子吗?fetch
不是React关键字。这是一个Javascript API。类似于XHR。看看这个@PiyushDhamecha没问题@Karthikeyan还可以查看此示例代码: