Dear All,
I'm novice to MVC2 and jquery, trying to use jquery.ajax to pass values in Model.
But I'm getting an error " [object xmlHttpRequest] 403 "
Here is my jquery code
var nameVal = $("#txtName").val();
var ageVal = $("#txtAge").val();
var phoneVal = $("#txtPhone").val();
alert(nameVal + " " + ageVal + " " + phoneVal + " ");
$.ajax({
type: "POST",
url: "/Models/OrderClass.cs/Insert",
data: "{name:'" + nameVal + "',age:'" + ageVal + "',phone:'" + phoneVal + "'}",
contentType: "application/json",
dataType: "json",
success: function(data) {
alert(data.d);
},
error: function(xmlHttpRequest, textStatus, errorThrown) {
if (xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0) {
alert(xmlHttpRequest + " " + xmlHttpRequest.status);
} else {
alert("ELSE :-) "+xmlHttpRequest + " " + xmlHttpRequest.status);
}
}
});
here is model code
[System.Web.Services.WebMethod]
[HttpPost]
public static int Insert(string name,string age, string phone)
{
int retVal = 0;
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(age) && !string.IsNullOrEmpty(phone))
{
try
{
using(MySqlConnection con = new MySqlConnection(ConfigurationManager.AppSettings["mvcIceCream"]))
{
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO student(Name,Age,Phone) VALUES('" + name + "','" + age + "','" + phone + "')";
cmd.CommandType = System.Data.CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
retVal = 1;
}
}
catch (Exception ex) { }
}
return retVal;
}
Please guide me.
Thanks