Click here to Skip to main content
15,891,689 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Posted

1 solution

Remove [HttpPost] from your model code
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900