我的代码有什么问题吗? 导致我运行它时出现错误部分
请帮助我 我还是这里的业余爱好者
<script type="text/javascript">
$(function() {
$('#btnRegister').click(function () {
var name = $('#inputFname').val();
var family = $('#inputLname').val();
var username = $('#inputUname').val();
var password = $('#inputPassword').val();
var mobile = $('#inputMobile').val();
var address = $('#inputEmail').val();
if (name !== '' && family !== '') {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Registeration.aspx.cs/InsertUser",
data: "{'FirstName':'" + name + "' , 'LastName' : '" + family + "' , 'UserName' :'" + username + "' , 'Password':'" + password + "', 'Mobile': '" + mobile + "','Address': '" + address + "'}",
dataType: "json",
success: function(data) {
$('#inputFname').value('a');
$('#inputLname').value('a');
$('#inputUname').value('a');
$('#inputPassword').value('a');
$('#inputMobile').value('a');
$('#inputEmail').value('a');
alert("Registeration Complete");
},
error: function(result) {
alert("Registeration Failed!");
}
});
} else {
alert("Please fill all fields!");
return false;
}
});
})
</script>
答案 0 :(得分:0)
您提供的是string
数据,而不是JSON
data: "{'FirstName':'" + name + "' , 'LastName' : '" + family + "' , 'UserName' :'" + username + "' , 'Password':'" + password + "', 'Mobile': '" + mobile + "','Address': '" + address + "'}"
您应将其更改为JSON
格式,
data: {
FirstName:name,
LastName:family,
UserName: username,
Password:password,
Mobile:mobile,
Address:address
}
该代码应类似于
$(function () {
$('#btnRegister').click(function () {
var name = $('#inputFname').val();
var family = $('#inputLname').val();
var username = $('#inputUname').val();
var password = $('#inputPassword').val();
var mobile = $('#inputMobile').val();
var address = $('#inputEmail').val();
if (name !== '' && family !== '') {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Registeration.aspx.cs/InsertUser",
data: {
FirstName:name,
LastName:family,
UserName: username,
Password:password,
Mobile:mobile,
Address:address
},
dataType: "json",
success: function (data) {
$('#inputFname').value('a');
$('#inputLname').value('a');
$('#inputUname').value('a');
$('#inputPassword').value('a');
$('#inputMobile').value('a');
$('#inputEmail').value('a');
alert("Registeration Complete");
},
error: function (result) {
alert("Registeration Failed!");
}
});
} else {
alert("Please fill all fields!");
return false;
}
});
})
答案 1 :(得分:0)
如果您的ajax转到了调用的错误部分,则响应URL处理有问题。好,通常至少可以做到。
console.log错误的结果,所以我们对问题有了一个认识
error: function(result) {
console.log(result);
}
banujan所说的也很有意义。
您还可以使用.serialize()从表单获取所有输入
var str = $( "form" ).serialize();
https://api.jquery.com/serialize/
编辑:
$(function() {
$('#btnRegister').click(function () {
var name = $('#inputFname').val();
var family = $('#inputLname').val();
var username = $('#inputUname').val();
var password = $('#inputPassword').val();
var mobile = $('#inputMobile').val();
var address = $('#inputEmail').val();
if (name !== '' && family !== '') {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Registeration.aspx.cs/InsertUser",
data: $( "#formId" ).serialize(),
success: function(data) {
$('#inputFname').value('a');
$('#inputLname').value('a');
$('#inputUname').value('a');
$('#inputPassword').value('a');
$('#inputMobile').value('a');
$('#inputEmail').value('a');
alert("Registeration Complete");
},
error: function(result) {
console.log(result)
//the result var in this case returns the response from the website. usually this holds a error of some kind
}
});
} else {
alert("Please fill all fields!");
return false;
}
});