fetch url form data는
"no=1&name=blabla" 이러한 형태임 json이랑 다른형태
사용시 주의해야할점! 기존에 json으로 되어있지않은지 점검
'Content-Type': 'application/x-www-form-urlencoded'
res.status성공(200)이면 return res.json() 안해도됨
function create_seeker() {
let strURL = '/api/주소'
let form = document.forms[0]
let form_data = new FormData(form)
let url_form_data = new URLSearchParams(form_data)
const init = {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'credentials': 'same-origin'
},
body: url_form_data
}
fetch(strURL, init)
.then(res => {
if (res.status == 200) {
alert('탐색기 추가 완료')
리스트api호출()
} else {
console.error(`HTTP ERROR! status${res.status}`)
}
})
.catch(err => {
console.log(err)
})
}
+
formdata로 추출한 데이터를 url_form_data로 바꾸기
let form = document.forms[0]
let form_data = new FormData(form)
let url_form_data = new URLSearchParams(form_data)
반응형
'Network&etc > HTTP' 카테고리의 다른 글
Failed to load resource: the server responded with a status of 404 (Not Found) (0) | 2022.06.21 |
---|---|
fetch와 axios 모듈화 예제 (0) | 2022.02.27 |
JSON.stringify(객체), JSON.parse(제이슨) (0) | 2021.09.28 |
ajax, fetch 예시(+form data) (0) | 2021.09.28 |
API 테스트 (0) | 2021.09.02 |