programing

Axios를 사용하여 객체를 JSon으로 게시

prostudy 2022. 5. 6. 19:45
반응형

Axios를 사용하여 객체를 JSon으로 게시

나는 이름이 붙은 물건을 가지고 있다.entry재산으로name,surname,age.

나는 사용하려고 노력한다.axios이 물건을 로 보내다post내 REST 서버에 요청.

axios.post('http://host/myurl/myservice/',{data:this.ent})

그러나 이것은 로 실패한다.

400 불량요청

왜냐하면 실제로 전송되고 있는 것은

 data: {data: {"name":"Jakob", "surname":"Laurence", "age":"25"} }

여분이 있다.data서버에서 인식되지 않는 필드

전화하면

axios.post('http://host/myurl/myservice/',{
"name":this.entry.name, "surname":this.entry.surname, "age":this.entry.age 
})

그러면 모든 것이 잘 된다.

서버가 혼동되지 않도록 추가 데이터 필드를 생성하지 않고 전체 개체를 게시하려면 어떻게 해야 하는가?

고마워요.

P.S. 위의 모든 것은 나의 Vue 프로젝트에서 진행되고 있다(관련성이 있는지는 확실치 않다).

전체 객체를 직접 통과:

axios.post('http://host/myurl/myservice/', this.ent);

데이터를 axios url에 추가하기 전에 상수로 정의하십시오.

const postData = {
  name: this.entry.name,
  surname: this.entry.surname,
  age: this.entry.age
}
axios.post('http://host/myurl/myservice/', postData)

참조URL: https://stackoverflow.com/questions/53516112/post-object-as-json-with-axios

반응형