我有一个简单的jquery表单,它从用户获取输入并将数据发布到另一个页面并显示成功消息。 但我的问题是数据没有发布到其他页面,而是弹出错误消息。
这是我的Jquery:
$(document).ready(function () {
$("#register").click(function () {
var name = $("#name").val();
var email = $("#email").val();
var password = $("#password").val();
var cpassword = $("#cpassword").val();
if (name == '' || email == '' || password == '' || cpassword == '') {
alert("Please fill all fields...!!!!!!");
} else if ((password.length) < 8) {
alert("Password should atleast 8 character in length...!!!!!!");
} else if (!(password).match(cpassword)) {
alert("Your passwords don't match. Try again?");
} else {
$.ajax({
type:"POST",
url: "SignUp.aspx/register", //trying to post to this url
data: { firstName: name },
success: function (msg) {
alert("Success"); // Success message which should pop up if successful
},
error: alert("Failed") //error message which should pop up if there is an error
});
}
});
});
以下是signup.aspx页面中的代码(类后面的代码):
public string register(String firstName)
{
return "Success";
}
我提供的网址是正确的,没有问题。 这可能是什么问题?
还有其他方法将数据发送到另一个页面吗?
感谢您的时间
答案 0 :(得分:1)
此
success: function (msg) {
alert("Success"); // Success message which should pop up if successful
},
error: alert("Failed") //error message which should pop up if there is an error
});
应该是:
success: function (msg) {
alert("Success"); // Success message which should pop up if successful
},
error: function(){alert("Failed")} //error message which should pop up if there is an error
});
但您可能会遇到其他错误。
答案 1 :(得分:1)
您的ajax调用格式不正确。它应该是:
$.ajax({
type:"POST",
url: "SignUp.aspx/register", //trying to post to this url
data: { firstName: name })
.done(function(data)
{
alert(JSON.stringify(data));
})
.success(function (msg) {
alert("Success"); // Success message which should pop up if successful
})
.fail(function(err){
alert("Failed"); //error message which should pop up if there is an error
});
答案 2 :(得分:0)
尝试设置ajax请求的contentType选项,
contentType:'application/x-www-urlencoded; UTF-8',
我希望能解决你的问题。