老师,我错哪里了
老师您好!
我跟源码兑了好几遍,地址也没错,就是报了这个错误;JSON.Parse is not a function at XMLHttpRequest.xhr.onreadystatechange
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="register">
<p class="title">
<span>登 录</span>
<span class="current">注 册</span>
</p>
<div class="form">
<div>
<span>+86</span>
<input type="text" name="user" id="user"
placeholder="请输入注册手机号" autocomplete="off" />
<i id="user_icon"></i>
<p class="info" id="user_info"></p>
</div>
<div>
<input type="password" name="pwd" id="pwd"
placeholder="请设置密码">
<i></i>
<p class="info"></p>
</div>
<p class="button">
<a href="javascript:void(0)" id="sigup-btn"
class="btn">注册</a>
</p>
</div>
</div>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript">
var user = document.getElementById("user"),
pwd = document.getElementById("pwd"),
sigup = document.getElementById("sigup-btn"),
userInfo = document.getElementById("user_info"),
userIcon = document.getElementById("user_icon");
// 检测用户
function checkUser(){
var userVal = user.value,
userReg = /^1[3578]\d{9}$/; // 手机号正则
// 验证手机号是否有效
if (!userReg.test(userVal)){
userInfo.innerHTML = "手机号码无效!";
userIcon.className = "no";
}else{
userInfo.innerHTML = "";
userIcon.className = "";
// 发起请求
$.ajax({
url:"http://localhost/ajax/register/server/isUserRepeat.php",
method:"post",
async:true,
data:{username:userVal},
success:function (data) {
console.log(data);
}
});
}
}
// 绑定事件,检测用户是否注册过
user.addEventListener("blur",checkUser, false);
</script>
</body>
</html>
----------------------------------------------------------------------------------------------------------------
var $ = {
ajax:function (options) {
var xhr = null,
url = options.url,
method = options.method || "get",
async = typeof(options.async) === "undefined"?true:options.async,
data = options.data || null,
params = "", // 传输参数
callback = options.success, // ajax请求成功的回调函数
error = options.error;
// 将data的对象字面量的形式转换为字符串形式
if (data){
for (var i in data){
params += i + "=" + data[i] + "&"; // i 为键, data[i]为 值
}
}
// 根据method的值改变url
if (method === "get"){
url += "?" + params;
}
if (typeof XMLHttpRequest != "undefined"){
xhr = new XMLHttpRequest();
}else if (typeof ActiveXObject != "undefined"){
// 将所有可能出现的ActiveXObject版本放在一个数组中
var xhrArr = ["Microsoft.XMLHTTP","MSXML2.XMLHTTP6.0",
"MSXML2.XMLHTTP5.0","MSXML2.XMLHTTP4.0",
"MSXML2.XMLHTTP3.0","MSXML2.XMLHTTP2.0"];
// 遍历创建XMLHttpRequest对象
var len = xhrArr.length;
for (var i = 0;i < len;i++){
try{
// 创建XMLHttpRequest对象
xhr = new ActiveXObject(xhrArr[i]);
break;
}
catch(ex){
}
}
}
else{
throw new Error("No XHR object availabel");
}
xhr.onreadystatechange = function(){
if (xhr.readyState === 4){
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status ===304) {
callback && callback(JSON.Parse(xhr.responseText));
}else{
error && error();
}
}
};
// 创建发送请求
xhr.open(method, url, async);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencode");
xhr.send(params);
}
// jsonp:function () { 也可以绑定跨域
//
// }
};
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星