【发布时间】:2013-09-18 00:38:20
【问题描述】:
我正在尝试使用 jquery ajax 调用将用户添加到数据库。用户可以很好地添加到数据库中,但 ajax 总是返回错误。我也不确定如何检索特定错误。下面是我的代码、表单、php 和 jquery。
这里是jquery
$(document).ready(function() {
//ajax call for all forms.
$('.button').click(function() {
var form = $(this).closest('form');
$.ajax({
type: "POST",
url: form.attr('data'),
dataType: 'json',
data: form.serialize(),
success: function (response) {
alert('something');
},
error: function() {
alert('fail');
}
});
});
});
这里是 PHP
<?php
include 'class_lib.php';
if(isset($_POST['username'])) {
$user = new Users;
$user->cleanInput($_POST['username'], $_POST['password']);
if($user->insertUser()) {
echo json_encode('true');
} else {
echo json_encode('false');
}
}
这里是 HTML
<div id='newUser' class='tool'>
<h3>New User</h3>
<form method='post' name='newUser' data='../php/newUser.php'>
<span>Username</span><input type='text' name='username'><br>
<span>Password</span><input type='password' name='password'>
<input type='submit' name='submit' class='button' style='visibility: hidden'>
</form>
<span class='result'> </span>
</div>
【问题讨论】:
-
我的 猜测 是它的解析错误,尝试删除
dataType: 'json',看看它是否有效。 -
似乎它仍然在失败中运行该功能
标签: javascript php jquery html ajax