Reputation: 612
This question seems copy of ASP.NET Core API POST parameter is always null this link , but i have tried all the options given for this question.
Mine axios call.
let finalData = JSON.stringify(data);
axios.post("url",{ headers: { "Content-Type": "application/json" }},{data:finalData});
finalData after JSON.stringyfy =
{"test1":{"name":"abc","id":1},"test2":{"name":"xyz","id":1}}
Core controller
[HttpPost]
public IActionResult doSomething(string data)
{
...mine logic
}
Upvotes: 0
Views: 701
Reputation: 684
You can use Formdata object:
let finalData = JSON.stringify(data);
var formData = new FormData();
fd.append( 'data', finalData);
$.ajax({
url: 'url',
data: formData,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
//TODO: use reponse from API
}
});
Hope you'll find this helpful.
Upvotes: 0
Reputation: 1
there's an error in the format you requested.
You can try this.
return axios.get(url, {
params: data,
headers: {'yl-authorization': this.token}//设置header信息
}).then((res) => {
})
Upvotes: 0
Reputation: 27528
I'm not sure why you don't want to use Model binding which help accept the values on server side easily . But if you want to get raw data and then deserialize it manually , one way is to do some custom processing of the Request.Body :
JS :
<script type="text/javascript">
$(function () {
var data = {
test1: {
name: 'abc',
id: 1
},
test2: {
name: 'xyz',
id: 1
}
}
axios.post("/Home/Test", data );
})
</script>
Sever side :
[HttpPost]
public async Task<IActionResult> Test()
{
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
var result = await reader.ReadToEndAsync();
}
.....
}
But i would always suggest use Model Binding instead .
Upvotes: 1