전번에는 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]
게시글 목록
| 번호 | 제목 |
|---|---|
| 17043 | |
| 17042 |
JavaScript
for 문으로 유사패턴 태그 만들기
4
|
| 17025 |
JavaScript
with 블럭 사용하기
17
|
| 17015 | |
| 17001 |
JavaScript
event listeners 찾기
3
|
| 16998 | |
| 16994 | |
| 16988 | |
| 16985 |
JavaScript
자바스크립트 for length / for in / for of
6
|
| 16983 | |
| 16981 | |
| 16979 | |
| 16978 | |
| 16976 | |
| 16968 | |
| 16967 |
JavaScript
상위요소 찾기 노드
3
|
| 16957 | |
| 16956 | |
| 16955 | |
| 16951 | |
| 16950 | |
| 16949 | |
| 16932 |
기타
postman
4
|
| 16930 | |
| 16913 | |
| 16909 | |
| 16906 | |
| 16905 | |
| 16904 | |
| 16894 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기