Axios에서 쿼리 파라미터를 게시하는 방법
몇 가지 쿼리 파라미터로 API에 글을 올리려고 합니다.이것은 메일과 이름을 쿼리 파라미터로 전달하려고 할 때 PostMan/Insumnia에서 작동합니다.
http://localhost:8000/api/mails/users/sendVerificationMail?mail=lol%40lol.com&firstname=myFirstName
그러나 리액트 네이티브 앱에서 실행하려고 하면 400 에러(Invalid Query Parameters)가 발생하였습니다.
포스트 방법은 다음과 같습니다.
.post(`/mails/users/sendVerificationMail`, {
mail,
firstname
})
.then(response => response.status)
.catch(err => console.warn(err));
(내 메일과 이름은 console.filename 입니다.lol@lol.com그리고.myFirstName).
그래서 요청에서 Axios를 사용하여 쿼리 파라미터를 전달하는 방법을 모르겠습니다(현재로서는 패스 중이기 때문에).data: { mail: "lol@lol.com", firstname: "myFirstName" }.
게시물의 악시오 서명은 다음과 같습니다.axios.post(url[, data[, config]])따라서 세 번째 인수 내에서 params 객체를 전송합니다.
.post(`/mails/users/sendVerificationMail`, null, { params: {
mail,
firstname
}})
.then(response => response.status)
.catch(err => console.warn(err));
그러면 다음 두 개의 쿼리 매개 변수가 있는 빈 본문이 POST됩니다.
POST http://localhost:8000/api/mails/users/sendVerificationMail?mail=http%40lol.com&firstname=myFirstName
2021년 현재 null을 설치했을 때 작동하기 위해 {}을(를) 추가해야 했습니다!
axios.post(
url,
{},
{
params: {
key,
checksum
}
}
)
.then(response => {
return success(response);
})
.catch(error => {
return fail(error);
});
제 경우 API가 CORS 오류로 응답했습니다.대신 쿼리 파라미터를 쿼리 문자열로 포맷했습니다.그것은 성공적으로 데이터를 게시했고 또한 CORS 문제도 피했다.
var data = {};
const params = new URLSearchParams({
contact: this.ContactPerson,
phoneNumber: this.PhoneNumber,
email: this.Email
}).toString();
const url =
"https://test.com/api/UpdateProfile?" +
params;
axios
.post(url, data, {
headers: {
aaid: this.ID,
token: this.Token
}
})
.then(res => {
this.Info = JSON.parse(res.data);
})
.catch(err => {
console.log(err);
});
언급URL : https://stackoverflow.com/questions/53501185/how-to-post-query-parameters-with-axios
'programing' 카테고리의 다른 글
| MariaDB가 시작되지 않음: "플러그인 'FEEDB'ACK'는 디세이블입니다. (0) | 2022.11.05 |
|---|---|
| Java에서 여러 수신인에게 메일 보내기 (0) | 2022.11.05 |
| Mysql - 저장 프로시저를 종료/종료하는 방법 (0) | 2022.11.05 |
| Google Maps API v3: fitBounds 후 줌을 설정할 수 있습니까? (0) | 2022.11.05 |
| Java Reflection을 사용하여 상속된 속성 이름/값 검색 (0) | 2022.11.05 |