猿问

?????

<!DOCTYPE html>

<html>


<head>

<meta charset="UTF-8">

<title>demo</title>

<style>

* {

font-size: 30px;

line-height: 1.8;

}

</style>

</head>


<body>

<h1>员工查询</h1>

<label>请输入员工的编号</label>

<input type="text" id="keyword" />

<button id="search">查询</button>

<p id="searchResult"></p>


<h1>员工新建</h1>

<label>请输入员工的姓名</label>

<input type="text" id="staffName" /><br />

<label>请输入员工的编号</label>

<input type="text" id="staffNumber" /><br />

<label>请输入员工的性别</label>

<select id="staffSex">

<option>男</option>

<option>女</option>

</select><br />

<label>请输入员工的职位</label>

<input type="text" id="staffJob" /><br />

<button id="save">保存</button>

<p id="createResult"></p>


<script>

document.getElementById('search').onclick = function() {

//发送Ajax查询请求并处理

var request = new XMLHttpRequest();

request.open('GET', 'service.php?number=' + document.getElementById('keyword').value);

request.send();

request.onreadystatechange = function() {

if(request.readyState === 4) {

if(request.status === 200) {

var data = JSON.parse(request.responseText);

if(data.success) {

document.getElementById("searchResult").innerHTML = data.msg;

} else {

document.getElementById("searchResult").innerHTML = '出现错误:' + data.msg;

}

} else {

alert("发生错误:" + request.status);

}

}

}

}

document.getElementById('save').onclick = function() {

//发送Ajax查询请求并处理

var request = new XMLHttpRequest();

request.open('POST', 'service.php');

var date = "name=" + document.getElementById('staffName').value +

"&number=" + document.getElementById('staffNumber').value +

"&sex=" + document.getElementById('staffSex').value +

"&job=" + document.getElementById('staffJob').value;

request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')

request.send(date);

request.onreadystatechange = function() {

if(request.readyState === 4) {

if(request.status === 200) {

var data = JSON.parse(request.responseText);

if(data.success) {

document.getElementById("createResult").innerHTML = data.msg;

} else {

document.getElementById("createResult").innerHTML = "出现错误:" + data.msg;

}

} else {

alert("发生错误:" + request.status);

}

}

}

}

</script>

</body>


</html>


点击查询出现这个:VM93:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0

点击保存就没出错

慕雪9687288
浏览 1404回答 2
2回答

夏依陌

多了一个}

HeyShinner

没有填写员工编号就直接点击查询就是会报错的吧 因为你没有添加相应的表单检查就直接发起ajax请求了 应该是这样……你试试
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答