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
}

3 Answers 3

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 .

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.