写jQuery ajax+PHP,用ajax提交“.html”的表单到“.php”,但是,传值一直出问题,提交表单总是报错:“SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data”。希望有大佬路过能救救萌新。下面是代码:
首先是html:
<form method="post">
<table border="0">
<tr>
<td>姓名:</td>
</tr>
<tr>
<td><input type="text" name="LoginName" maxlength="7" /></td>
</tr>
<tr>
<td>密码:</td>
</tr>
<tr>
<td><input type="text" name="LoginPassword" maxlength="15"/></td>
</tr>
</table>
<button id="LoginSubmit">Start</button>
</form>
接着是js:
$('#LoginSubmit').click(function () {
var LoginInput = $('#Login input');
var data = {"LoginName":LoginInput.eq(0).val(), "LoginPassword":LoginInput.eq(1).val()};
$.ajax({
type:"post",
url:"../php/Login.php",
async:true,
contentType:'application/json; charset=UTF-8',
dataType:'json',
data:JSON.stringify(data),
success:function (respon) {
alert(respon);
},
error: function() {
alert("失败");
}
});
window.location.href = "../php/Login.php";
return false;
});
最后是PHP:
<?php
header('Content-type: application/json');
$name = $_POST['LoginName'];
$password = $_POST['LoginPassword'];
if($name == "user"){
echo "1";
}else{
echo "0";
}
?>
我用的是三个文件,“.html”、“.js”“.php”,看了很多解决方法不是涉及后端,就是没有用“.html”来做页面。也试过了很多方法,弄了三天还是没有解决。。。。。。还有萌新学生初来乍到,真的没有悬赏币,但是还是恳求有大佬来解答。