반응형
가져오기 요청에서 POST 매개 변수를 전달하는 방법 - 기본 반응
이건 내 암호야
componentWillMount() {
fetch("http://localmachine/localservice/webservice/rest/server.php", {
method: 'POST',
body: JSON.stringify({
wstoken: 'any_token',
wsfunction: 'any_function',
moodlewsrestformat: 'json',
username: 'user',
password: 'pass',
})
})
.then((response) => response.text())
.then((responseText) => {
alert(responseText);
})
.catch((error) => {
console.error(error);
});
}
브라우저에서는 이 요청이 토큰을 반환하지만, 내 반응형 안드로이드 앱에서는 xml 오류를 반환한다.
이것이 나에게 효과가 있었다.
fetch("http://10.4.5.114/localservice/webservice/rest/server.php", {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/x-www-form-urlencoded', // <-- Specifying the Content-Type
}),
body: "param1=value1¶m2=value2" // <-- Post parameters
})
.then((response) => response.text())
.then((responseText) => {
alert(responseText);
})
.catch((error) => {
console.error(error);
});
사후 요청에 헤더를 추가해 보십시오.
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
wstoken: 'any_token',
wsfunction: 'any_function',
moodlewsrestformat: 'json',
username: 'user',
password: 'pass',
})
반응형
'IT이야기' 카테고리의 다른 글
React.useImperativeHandle()로 유형을 선언하십시오. (0) | 2022.03.27 |
---|---|
과학적 표기법 없이 정밀한 방법으로 숫자 배열로 예쁘게 인쇄하는 방법? (0) | 2022.03.27 |
Nuxt.js vuex 스토어가 유지되지 않음 (0) | 2022.03.27 |
관찰 가능한 테이크아웃 취소작동하지 않을 때까지 (0) | 2022.03.27 |
각각은 JavaScript 배열의 함수 오류가 아님 (0) | 2022.03.27 |