• 카테고리

    질문 & 답변
  • 세부 분야

    프론트엔드

  • 해결 여부

    미해결

안녕하세요 강사님

19.06.05 16:28 작성 조회수 156

0

질문이  있습니다.

board를 add 할때 api/index.js 파일에서  

board -> create() 를 생성하셨을 때 data해당값에 title을 넘기셨는데, 

세번 쨰 인자를 아래와 같이 넣으면 오류가 나고,

강사님 코드 보니깐 {title}이런식으로 넣으면 오류가 해결이 되더라구요?

이게 문법 적으로 틀려서 오류가 나는건지, 아니면 데이터를 넘길 때 json과 같이 넘기는 형식이 따로 있어서 오류가 나는건지 궁금해서 질문 드립니다.

create(title) {
return request('post', '/boards', title)
}
 

답변 1

답변을 작성해보세요.

1

linesay님의 프로필

linesay

2020.09.30

안녕하세요. 예전에 올려주신 거라 이미 답을 찾으셨을 것 같은데 다른 분들도 궁금해 하실 것 같아서 올립니다.

'{}' 기호는 객체라고 표현하는 객체 리터럴으로 알고 있습니다. axios에서 post,delete,put,patch 등의 request method를 이용하여 데이터를 전달할 때는 객체로 전달해야 하는 것 같습니다.

https://www.npmjs.com/package/axios#request-method-aliases

// Send a POST request
axios({
  method: 'post',
  url: '/user/12345',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  }
});
  // `data` is the data to be sent as the request body
  // Only applicable for request methods 'PUT', 'POST', 'DELETE , and 'PATCH'
  // When no `transformRequest` is set, must be of one of the following types:
  // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
  // - Browser only: FormData, File, Blob
  // - Node only: Stream, Buffer
  data: {
    firstName: 'Fred'  },