html页面:
<code> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <form action="reg.php" method="post"> 用户名 : <input id="input1" type="text" name="username"> <input type="submit" value="注册"> </form> <div id="div1"></div> <script> var oInput = document.getElementById('input1'); var oDiv = document.getElementById('div1'); oInput.onblur = function(){ var xhr = new XMLHttpRequest(); xhr.open('GET','ajax.php?username='+encodeURIComponent(this.value),true); xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ if(xhr.status == 200){ var obj = JSON.parse(xhr.responseText); if(obj.code){ oDiv.innerHTML = obj.message; } else{ oDiv.innerHTML = obj.message; } } } }; xhr.send(null); }; </script> </code>
php页面:
<code><?php header('Content-type:text/html;charset=utf-8'); $conn = mysqli_connect('127.0.0.1', 'root', '5221107073', 'linshi'); $name = $_GET['username']; $sql = "SELECT * FROM shi where name='$name'"; $res = mysqli_query($conn, $sql); if($res && mysqli_num_rows($res)){ echo "{'code':'0','message':'该名字有人注册'}"; }else{ echo "{'code':'1','message':'ok'}"; } ?></code>
就是不能从服务端获取到json数据,报错如下:
<code>SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data</code>
求解
html页面:
<code> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <form action="reg.php" method="post"> 用户名 : <input id="input1" type="text" name="username"> <input type="submit" value="注册"> </form> <div id="div1"></div> <script> var oInput = document.getElementById('input1'); var oDiv = document.getElementById('div1'); oInput.onblur = function(){ var xhr = new XMLHttpRequest(); xhr.open('GET','ajax.php?username='+encodeURIComponent(this.value),true); xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ if(xhr.status == 200){ var obj = JSON.parse(xhr.responseText); if(obj.code){ oDiv.innerHTML = obj.message; } else{ oDiv.innerHTML = obj.message; } } } }; xhr.send(null); }; </script> </code>
php页面:
<code><?php header('Content-type:text/html;charset=utf-8'); $conn = mysqli_connect('127.0.0.1', 'root', '5221107073', 'linshi'); $name = $_GET['username']; $sql = "SELECT * FROM shi where name='$name'"; $res = mysqli_query($conn, $sql); if($res && mysqli_num_rows($res)){ echo "{'code':'0','message':'该名字有人注册'}"; }else{ echo "{'code':'1','message':'ok'}"; } ?></code>
就是不能从服务端获取到json数据,报错如下:
<code>SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data</code>
求解
header的设置不对,这样设置输出的是utf-8格式的html,
使用
<code>header('Content-type: application/json');</code>
这样echo的数据即为json格式,
建议将要输出的内容存进数组,在要输出的地方使用
<code>echo json_encode($array);</code>
没试过,你试试不要直接这么写,换个写法,定义一个数组,然后json_encode()。
后端返回的格式不对
<code class="php">echo '{"code":"0","message":"该名字有人注册"}'</code>
格式不对,json里面是双引号的。
这种
<code>echo '{"code":"0","message":"该名字有人注册"}'</code>
我在前台页面把从服务端获取到的结果输出`if(xhr.readyState == 4){
<code> if(xhr.status == 200){ console.log(xhr.responseText); console.log(JSON.parse(xhr.responseText)); } }`</code>
在控制台看到是这样的(就是说xhr.responseText获取有问题吗?):