0

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
}
Sandeep Rasgotra
  • 582
  • 1
  • 7
  • 18

3 Answers3

1

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 .

Nan Yu
  • 26,101
  • 9
  • 68
  • 148
0

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) => {
        })
0

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.

KYG
  • 614
  • 3
  • 9