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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4946 | node.js | 6년 전 | 2610 | ||
| 4945 | node.js | 6년 전 | 2382 | ||
| 4944 | node.js | 6년 전 | 2520 | ||
| 4943 | node.js | 6년 전 | 2272 | ||
| 4942 | node.js | 6년 전 | 2250 | ||
| 4941 | node.js | 6년 전 | 2720 | ||
| 4940 | node.js | 6년 전 | 1867 | ||
| 4939 | node.js | 6년 전 | 1995 | ||
| 4938 | node.js | 6년 전 | 2459 | ||
| 4937 | node.js | 6년 전 | 2250 | ||
| 4936 | node.js | 6년 전 | 2322 | ||
| 4935 | node.js | 6년 전 | 2136 | ||
| 4934 | node.js | 6년 전 | 2442 | ||
| 4933 | node.js | 6년 전 | 2247 | ||
| 4932 | node.js | 6년 전 | 2687 | ||
| 4931 | node.js | 6년 전 | 2070 | ||
| 4930 | node.js | 6년 전 | 1999 | ||
| 4929 | node.js | 6년 전 | 8632 | ||
| 4928 | node.js | 6년 전 | 3750 | ||
| 4927 | node.js | 6년 전 | 2394 | ||
| 4926 | node.js | 6년 전 | 2504 | ||
| 4925 | node.js | 6년 전 | 2084 | ||
| 4924 | node.js | 6년 전 | 3374 | ||
| 4923 | node.js | 6년 전 | 2221 | ||
| 4922 | node.js | 6년 전 | 1991 | ||
| 4921 | node.js | 6년 전 | 2046 | ||
| 4920 | node.js | 6년 전 | 1765 | ||
| 4919 | node.js | 6년 전 | 2032 | ||
| 4918 | node.js | 6년 전 | 2180 | ||
| 4917 | node.js | 6년 전 | 2396 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기