본문 바로가기
develop/java script

ajax 패턴

by hybr1d 2018. 12. 20.

Examples:

Save some data to the server and notify the user once it's complete.

1
2
3
4
5
6
7
8
$.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});

Retrieve the latest version of an HTML page.

1
2
3
4
5
6
7
$.ajax({
url: "test.html",
cache: false
})
.done(function( html ) {
$( "#results" ).append( html );
});

Send an xml document as data to the server. By setting the processData option to false, the automatic conversion of data to strings is prevented.

1
2
3
4
5
6
7
8
var xmlDocument = [create xml document];
var xmlRequest = $.ajax({
url: "page.php",
processData: false,
data: xmlDocument
});
xmlRequest.done( handleResponse );

Send an id as data to the server, save some data to the server, and notify the user once it's complete. If the request fails, alert the user.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var menuId = $( "ul.nav" ).first().attr( "id" );
var request = $.ajax({
url: "script.php",
method: "POST",
data: { id : menuId },
dataType: "html"
});
request.done(function( msg ) {
$( "#log" ).html( msg );
});
request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});

Load and execute a JavaScript file.

1
2
3
4
5
$.ajax({
method: "GET",
url: "test.js",
dataType: "script"
});


'develop > java script' 카테고리의 다른 글

ES6  (0) 2020.05.25
jquery 배열 object 삭제  (0) 2020.05.21
Google javascript style guide  (0) 2018.04.05
api ajax script  (0) 2018.02.28
웹에서 문자 보낼 때 80바이트 이상 못 쓰게 하는 자바스크립트  (0) 2017.11.21