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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5366 | 기타 |
nekoieye
|
1년 전 | 1110 | |
| 5365 | PHP | 1년 전 | 769 | ||
| 5364 | JavaScript | 1년 전 | 520 | ||
| 5363 | JavaScript | 1년 전 | 986 | ||
| 5362 | 1년 전 | 1225 | |||
| 5361 | JavaScript | 1년 전 | 1231 | ||
| 5360 |
그누보드이해하기
|
1년 전 | 769 | ||
| 5359 | 1년 전 | 1085 | |||
| 5358 | 1년 전 | 1090 | |||
| 5357 | 1년 전 | 803 | |||
| 5356 | 1년 전 | 365 | |||
| 5355 |
|
1년 전 | 582 | ||
| 5354 | 1년 전 | 1055 | |||
| 5353 |
|
1년 전 | 700 | ||
| 5352 |
|
1년 전 | 634 | ||
| 5351 | PHP | 1년 전 | 801 | ||
| 5350 | PHP | 1년 전 | 568 | ||
| 5349 | OS | 1년 전 | 642 | ||
| 5348 | 기타 | 1년 전 | 446 | ||
| 5347 | 기타 | 1년 전 | 625 | ||
| 5346 | 기타 | 1년 전 | 502 | ||
| 5345 | 기타 | 1년 전 | 393 | ||
| 5344 | JavaScript |
|
1년 전 | 422 | |
| 5343 | PHP |
|
1년 전 | 493 | |
| 5342 | jQuery | 1년 전 | 568 | ||
| 5341 | PHP |
|
1년 전 | 870 | |
| 5340 | Mobile |
슈퍼스타맨
|
1년 전 | 547 | |
| 5339 | 기타 |
|
1년 전 | 545 | |
| 5338 |
멀티트리플
|
2년 전 | 1288 | ||
| 5337 | 기타 |
슈퍼스타맨
|
2년 전 | 1780 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기