我正在尝试连接到服务器以登录,但是在发送验证请求时,状态(状态更改)保持为1(建立连接),然后丢失。 显然,onreadystatechange仅运行一次,但是我不知道为什么,并且进行了调查,但是我发现的信息不是我的情况。
var peticion
var net = {}
var strQuery
/* global XMLHttpRequest ActiveXObject */
window.onload = function () {
var btn = document.getElementById('comprobar')
btn.addEventListener('click', validate)
strQuery = 'login=' + encodeURIComponent(document.getElementById('login').value + '&nocache=' + Math.random())
}
function validate () {
peticion = new net.CargarContenidos('http://localhost/Paginas/AJAXTest/servidor/compruebaDisponibilidad.php', mostrar, strQuery)
}
function mostrar () {
console.log('si entro')
console.log(peticion.req.responseText)
}
net.READY_STATE_UNINITIALIZAED = 0
net.READY_STATE_LOADING = 1
net.READY_STATE_LOADED = 2
net.READY_STATE_INTERACTIVE = 3
net.READY_STATE_COMPLETE = 4
net.CargarContenidos = function (url, funcion, data, funcionError) {
this.url = url
this.req = null
this.onload = funcion
this.onerror = this.defaultError
this.cargaContenidosXML(url, data)
console.log('finish')
}
net.CargarContenidos.prototype = {
cargaContenidosXML: function (url, data) {
if (window.XMLHttpRequest) {
this.req = new XMLHttpRequest()
console.log(this.req)
} else if (window.ActiveXObject) {
this.req = new ActiveXObject('Microsoft.XMLHTTP')
}
if (this.req) {
try {
var loader = this
this.req.onreadystatechange = function () {
loader.onReadyState()
}
this.req.open('POST', url, true)
this.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
this.req.send(data)
} catch (err) {
alert('error: ' + err)
}
}
},
onReadyState: function () {
var req = this.req
var ready = req.readyState
console.log(ready)
if (ready == net.READY_STATE_COMPLETE) {
var httpStatus = req.status
if (httpStatus == 200 || httpStatus == 0) {
this.onload()
}
}
},
}
我是一名学生,我非常感谢您的建议,在此先感谢