I need to send an ajax request to a post method defined in my asp.net core web api as below :
// POST: api/Entreprise/Inscription
[HttpPost("Inscription")]
public IActionResult Post([FromBody] UserInfos value)
{
return Ok("value 1");
}
and this is UserInfos model:
public class UserInfos
{
public string firstname { get; set; }
public string lastname { get; set; }
public string email { get; set; }
public string domainName { get; set; }
public string phoneNumber {get;set;}
public string address { get; set; }
public string city { get; set; }
public string zip_code { get; set; }
}
I tested it with postman , by setting the header as 'Content-Type':'application/json' and in the body choosed raw and passed this json object :
{
"firstname" :"ahmed",
"lastname":"haddad",
"email":"haddad-a@live.fr" ,
"domainName":"easyappointments-master" ,
"phoneNumber":"25276164",
"address":"ariana" ,
"city":"grand tunis",
"zip_code":"4100"
}
and i get it to work, however when i call it from ajax i get BAD REQUEST 400 , this is my ajax code:
var newData={
"firstname" :"ahmed",
"lastname":"haddad",
"email":"haddad-a@live.fr" ,
"domainName":"easyappointments-master" ,
"phoneNumber":"25276164",
"address":"ariana" ,
"city":"grand tunis",
"zip_code":"4100" ,
};
var dataJson= JSON.stringify(newData);
$.ajax({
url:'http://test.example.fr/wsexample/api/Entreprise/Inscription',
dataType:'json',
data:dataJson,
ContentType:'application/json',
type:'post',
success:function(data,status){
console.log('the request is '+status+' the data is '+data);
},
error:function(html,status,error){
console.log('the request is '+error);
}
});
Note: the asp.net core web api and the ajax codes are in different servers ,so different domains , i have enabled the access to CORS for my domain in startup.cs , so normally that shouldn't trigger an issue. also i have made succeded get requests to that webservice