테스트 버튼을 클릭하면 결과가 나타나지 않으며 오류도 발생하지 않습니다. 당신이 아마 볼 수 있듯이 나는 자바 스크립트에서 매우 초보자이다. 어떤 솔루션을 권하고 싶습니다. 그래서이 "수업"을 쓸 수 있습니까? 나는 그것이 jQuery ajax와 같이 더 많이 일하기를 원한다. $ .ajax ({}); ... 자바 스크립트 오버라이드 객체 속성
var Ajax = function(){
this.method = null;
this.url = null;
this.headerType = null;
this.data = null;
function request (callback) {
var xml = new XMLHttpRequest();
xml.open(this.method, this.url, true);
xml.setRequestHeader(this.headerType || "Content-type", "application/x-www-form-urlencoded");
xml.onreadystatechange = function() {
if(xml.readyState == 4) {
if(xml.status == 200) {
callback(xml.responseText);
}
}
}
xml.send(this.data || null);
}
}
document.getElementById('test').addEventListener('click', function() {
Ajax({
method : 'GET',
url : 'test.php',
request : function(response) {
document.getElementById('testResult').innerHTML = response;
}
});
});
당신에게
편집 감사합니다 : 여기에 HTML 코드
<button id="test">Get data</button>
<div id="testResult"></div>
우리는 HTML 코드를 볼 수 있습니다 자바 스크립트 클래스의
일반 개념이 무엇입니까? 이벤트가 버튼에 올바르게 설정되어 있습니까? – Gwenc37
함수 호출은 마술처럼 객체를 인수로 취해 적용하지 않습니다. 당신은 그것을 코드화해야합니다 ... – epascarello
@ Gwenc37 html을 볼 필요가 없습니다. – epascarello