jQuery없이 네티브자바스크립트로 서버에 요청보내기2 - POST
전번에는 GET을 구현했습니다. 이번 시간에는 POST를 네티브자바스크립트로 구현하는 방법을 보여드립니다.
[code]
// jQuery
var newName = 'John Smith';
$.ajax('myservice/username?' + $.param({id: 'some-unique-id'}), {
method: 'POST',
data: {
name: newName
}
})
.then(
function success(name) {
if (name !== newName) {
alert('Something went wrong. Name is now ' + name);
}
},
function fail(data, status) {
alert('Request failed. Returned status of ' + status);
}
);
// Native XMLHttpRequest Object
var newName = 'John Smith', xhr = new XMLHttpRequest();
xhr.open('POST', 'myservice/username?id=some-unique-id');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
if (xhr.status === 200 && xhr.responseText !== newName) {
alert('Something went wrong. Name is now ' + xhr.responseText);
}
else if (xhr.status !== 200) {
alert('Request failed. Returned status of ' + xhr.status);
}
};
xhr.send(encodeURI('name=' + newName));
[/code]
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4916 | 웹서버 | 6년 전 | 2870 | ||
| 4915 | PHP |
|
6년 전 | 2187 | |
| 4914 | 웹서버 | 7년 전 | 1899 | ||
| 4913 | JavaScript | 7년 전 | 2601 | ||
| 4912 | node.js | 7년 전 | 3717 | ||
| 4911 | 기타 | 7년 전 | 4194 | ||
| 4910 | 기타 | 7년 전 | 2279 | ||
| 4909 | 기타 | 7년 전 | 1986 | ||
| 4908 | 기타 | 7년 전 | 2022 | ||
| 4907 | Mobile | 7년 전 | 2293 | ||
| 4906 | JavaScript | 7년 전 | 2294 | ||
| 4905 | 기타 | 7년 전 | 2274 | ||
| 4904 | jQuery | 7년 전 | 2522 | ||
| 4903 | PHP | 7년 전 | 5232 | ||
| 4902 | jQuery | 7년 전 | 5132 | ||
| 4901 | 기타 | 7년 전 | 2713 | ||
| 4900 | MySQL | 7년 전 | 4122 | ||
| 4899 | 기타 | 7년 전 | 2268 | ||
| 4898 | 웹서버 | 7년 전 | 2453 | ||
| 4897 | MySQL | 7년 전 | 2210 | ||
| 4896 | MySQL | 7년 전 | 2637 | ||
| 4895 | JavaScript | 7년 전 | 9776 | ||
| 4894 | 웹서버 | 7년 전 | 2368 | ||
| 4893 | 기타 | 7년 전 | 8323 | ||
| 4892 | jQuery | 7년 전 | 5690 | ||
| 4891 | 기타 | 7년 전 | 2839 | ||
| 4890 | PHP | 7년 전 | 3424 | ||
| 4889 | JavaScript | 7년 전 | 6378 | ||
| 4888 | MySQL | 7년 전 | 3140 | ||
| 4887 | MySQL | 7년 전 | 2768 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기