我正在创建一个验证注册并使用Ajax将其发送到PHP脚本的函数,但它只能工作一半 - 它无法通过Ajax发送用户名和密码值
那么,下面我的Ajax功能有什么问题?
function register(btregister)
{
username = $.trim($('#username').val());
if (!validateUsername(username)) {
$('#username').focus();
setTimeout(function() {$(btregister).removeAttr('disabled');}, 2000);
return;
}
password=$('#password').val();
if (password == '') {
$('#password').focus();
setTimeout(function() {$(btregister).removeAttr('disabled');}, 2000);
return;
}
// it's stop working until here.
$.ajax({
type: 'POST',
url: "http://localhost/system/controllers/ajax_register.php",
data: 'un=' + username + '&pw=' + password,
success: function(resp) {
alert(resp);
},
error: function() {
}
});
}
答案 0 :(得分:0)
尝试将数据指定为对象:
$.ajax({
type: 'POST',
url: "http://localhost/system/controllers/ajax_register.php",
contentType: 'application/json; charset=utf-8',
data: "{ 'un':'"+username+"', 'pw':'"+password+"' }",
dataType: 'json',
success: function(resp) {
alert(resp);
},
failure: function() {
}
});