Cannot request API when using axios call

the dotnet core webapi code is as follows

        [HttpGet]
        [EnableCors("CMSCorsConfig")]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        [HttpGet("{id}")]
        [EnableCors("CMSCorsConfig")]
        public ActionResult<string> Get(int id)
        {
            return "value";
        }

        // POST api/values
        [HttpPost]
        [EnableCors("CMSCorsConfig")]
        public void Post([FromBody] string value)
        {
            //throw new Exception("HELLO FROM POST");
        }

axios request code is as follows:

export function write (title, name) {
  return axios({
    url: "/values/",
    method: "post",
    data: { title, name }
  })
}

the error is as follows:

I succeeded in calling axios to another API,

export function write (title, name) {
  return axios({
    url: "/values/",
    method: "get"    
  })
}

I can succeed with the following rewriting, but I can"t get the value

        [HttpPost]
        [EnableCors("CMSCorsConfig")]
        public string Post( string title,string name)
        {
            return title;
        }

can be called successfully if changed to this way, but the return value is always null

how to adjust, and under what circumstances is [FromBody] used


is the form form receiving data in the background? it should be url transmission when you use get request, and the data format received in the background is similar to 'id=1&from=home'' format, but if you use post to transmit the data format is key-value pair: {id: 1, from: "home"}, the background can not receive it.

if the backend number is received by the form form, use the 'qs' module `import axios from' axios';
import {getToken} from'. / auth'
/ / import 'url-search-params-polyfill'
import qs from' qs'

in the axios interceptor.

axios.defaults.timeout = 5000
axios.defaults.baseURL = process.env.BASE_API

/ / http request interceptor
axios.interceptors.request.use (
config = > {

)
const token = getToken() // cookiejs-cookie
// config.data = new URLSearchParams(config.data) // { id: 1, from: "home" }'id=1&from=home'
config.data = qs.stringify(config.data)
console.log('')
config.headers = {
  'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
  'client_type': '20',
  'api_version': '1.0.0000',
  'token': token || ''
}
return config;

},
error = > {

return Promise.reject(err);

}
); `


if it is a single string,client, it should be written as {"": value}
if it is multiple fields, the server needs to encapsulate it into object, plus frombody.
according to the way you write it, it is fromuri to get title and so on.

bad request problems you can debug in vs output specific error to see.

Menu