먼저, 귀하의 참조를위한 기본 JavaScript 구현 Ajax 코드를 공유했습니다. 특정 내용은 다음과 같습니다
var getxmlhttprequest = function () {if (wind } else if (wind }}; var xhr = getxmlhttprequest (); xhr.onreadyStateChange = function () {console.log (xhr.readystate); if (xhr.readystate === 3 && xhr.status === 200) {// 성공적인 획득 후 작업을 실행합니다. // 데이터는 xhr.responsetext console.log (xhr.responsetext)에 있습니다. }}; xhr.open ( "get", "data.php", true); xhr.send ( "");아래는 JavaScript를 사용하여 기본 AJAX를 구현하는 몇 가지 방법을 공유하겠습니다.
ajax를 구현하기 전에 xmlhttprequest 객체를 만들어야합니다. 이 객체를 작성하는 브라우저가 지원되지 않으면 ActiveXobject를 작성해야합니다. 특정 방법은 다음과 같습니다.
var xmlhttp; 함수 createxmlhttprequest () {if (wind } else if (wind }(1) 다음은 위에서 만든 XMLHTTP를 사용하여 가장 간단한 AJAX GET 요청을 구현합니다.
function doget (url) {// 매개 변수 값을 전달할 때 encodeuri를 사용하여 처리하는 것이 가장 좋습니다. xmlhttp.open ( "get", url); xmlhttp.send (null); xmlhttp.onreadyStateChange = function () {if ((xmlhttp.readystate == 4) && (xmlhttp.status == 200)) {alert ( 'success'); } else {alert ( 'fail'); }}}(2) 위에서 만든 XMLHTTP를 사용하여 가장 간단한 AJAX 게시물 요청을 구현하십시오.
function dopost (url, data) {// 매개 변수 값을 전달할 때 encodeuri를 사용하여 처리하는 것이 가장 좋습니다. xmlhttp.open ( "post", url); xmlhttp.setrequestheader ( "content-type", "application/x-www-form-urlencoded"); xmlhttp.send (데이터); xmlhttp.onreadyStateChange = function () {if ((xmlhttp.readystate == 4) && (xmlhttp.status == 200)) {alert ( 'success'); } else {alert ( 'fail'); }}}위의 내용은이 기사에 관한 모든 것입니다. 모든 사람의 학습에 도움이되기를 바랍니다.