这是我的 js 文件,其中我有一个意外的 token <错误,我已验证来自 jshint 的代码,没有任何内容可以解决该问题。
我尝试将js放入不同的文件中,也尝试将html放入同一文件中,但没有解决错误仍然存在
function ajax() {
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
return XMLHttpRequestObject;
}
function login() {
var wait = document.getElementById('wait-img').style;
wait.visibility = "visible";
document.getElementById('fade').style.display = 'block';
var email = document.getElementById('username').value;
var password = document.getElementById('password').value;
if (email === '' || password === '') {
document.getElementById('error').innerHTML = 'Please fill out all details';
wait.visibility = "hidden";
document.getElementById('fade').style.display = 'none';
return;
}
var data = "email=" + email + "&password=" + password + "&x=login";
var xml = ajax();
if (xml) {
xml.open("POST", "login1.php");
xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xml.onreadystatechange = function() {
if (xml.readyState == 4 && xml.status == 200) {
var reply = JSON.parse(xml.responseText);
if (reply == 'failure') {
document.getElementById('error').innerHTML = 'Email or password is incorrect';
} else
window.location = 'profile.php?id=' + reply;
wait.style.visibility = "hidden";
document.getElementById('fade').style.display = 'none';
}
};
xml.send(data);
}
}
function signup() {
var wait = document.getElementById('wait-img').style;
wait.visibility = "visible";
document.getElementById('fade').style.display = 'block';
var email = document.getElementById('email').value;
var password = document.getElementById('psswd').value;
var phone = document.getElementById('phone').value;
var confirm = document.getElementById('confirm').value;
if (email === '' || password === '' || phone === '' || confirm === '') {
document.getElementById('error').innerHTML = 'Please fill out all details';
wait.visibility = "hidden";
document.getElementById('fade').style.display = 'none';
return;
}
if (password != confirm) {
document.getElementById('error').innerHTML = 'Password did not match';
wait.visibility = "hidden";
document.getElementById('fade').style.display = 'none';
return;
}
var data = "email=" + email + "&password=" + password + "&phone=" + phone + "&x=signup";
var xml = ajax();
if (xml) {
xml.open("POST", "signup.php");
xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xml.onreadystatechange = function() {
if (xml.readyState == 4 && xml.status == 200) {
var reply = JSON.parse(xml.responseText);
if (reply == 'failure') {
document.getElementById('error').innerHTML = 'Sorry something went wrong';
} else
window.location = 'profile.php?id=' + reply;
wait.visibility = "hidden";
document.getElementById('fade').style.display = 'none';
}
};
xml.send(data);
}
}
更新:错误出现在该文件的第 41 行。
最佳答案
错误必须出现在您正在导入的 JSON 文件中(在 JSON.parse(xml.responseText);
使用 try-catch 确保导入的文件具有正确的 JSON 语法。
尝试{
var回复=JSON.parse(xml.responseText);
} 捕获(e){
console.log(e,xml.responseText)
}
这将帮助您调试错误。
关于javascript - javascript 中的意外标记 <,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35594708/