我试图在asp.net中使用ajax调用webservice方法。 这是我的网络服务方法。
[WebMethod]
public String registerWS(String email, String firstname, String lastname, String address, String mobile)
{
String query = "insertcrud_sp";
SqlCommand com = new SqlCommand(query, con);
com.CommandType = CommandType.StoredProcedure;
String res = "";
com.Parameters.AddWithValue("@email", email);
com.Parameters.AddWithValue("@firstname", firstname);
com.Parameters.AddWithValue("@lastname", lastname);
com.Parameters.AddWithValue("@address", address);
com.Parameters.AddWithValue("@mobile", mobile);
con.Open();
if (com.ExecuteNonQuery() > 0)
{
con.Close();
res= "true";
}
else
{
con.Close();
res= "false";
}
return res;
}
这是我的jquery代码..
$(document).on("click","#btnajaxcontrol", function () {
var mail = document.getElementById("txtmail").value;
var fname = document.getElementById("txtfname").value;
var lname = document.getElementById("txtlname").value;
var addr = document.getElementById("txtaddress").value;
var mobile = document.getElementById("txtmobile").value;
if (ValidateEmail(mail) && !(mail == "") && !(fname == "") && !(lname == "") && !(addr == "") && !(mobile == "")) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "CRUDwebserviceJS.asmx/registerWS",
data: { 'email': mail, 'firstname': fname, 'lastname': lname, 'address': addr, 'mobile': mobile },
cache: false,
success: function (data) {
var wsres = data.d;
if (wsres == 'true') {
$('#lblmsg').html('User Successfully Registered...');
}
},
error: function (result) {
alert(result);
$('#lblmsg').html('User Registeration faild...!');
$("#lblmsg").css('background', 'red');
}
});
}
});
Web服务方法单独工作......但是当我从ajax调用web服务时,只调用了错误消息...我不知道我的代码有什么问题..请帮我找出什么错误在我的程序中,并建议我需要改变....感谢高级....