javascript - invoking method using ajax at code behind in asp.net -
hey making simple web form product detail insertion web page. trying insert using ajax call. without ajax works .. $.ajax not invoking code behind static method, no idea wat's issue. here's code:
$(document).ready(function () { $("#submit").click(function () { var cat = document.getelementbyid('dropdownlist1').value; var nm = document.getelementbyid('name').value; var cde = document.getelementbyid('code').value; var dt = document.getelementbyid('dt').value; var price = document.getelementbyid('price').value; var f3 = document.getelementbyid('ty').innerhtml; alert("you clicked " + cat + " - " + nm + "-" + cde + "-" + dt + "-" + price + "-" + f3 + "-"); //////////////uptil here alert gives right value. $.ajax({ method: "post", contenttype: "application/json", url: "home.aspx/ins", datatype: "json", data: "{'name :' + nm + 'code :' + cde +'category :'+ cat + 'date :'+ dt +'price :'+ pr +'img_name :' + f3}", //data:"{}", //async: false, success: function (response) { alert("user has been added successfully."); window.location.reload(); } }); }) });
//////////////////////////////// here code behind method: [system.web.services.webmethod] public static void ins(string name,string code,string category, datetime date, int price,string img_name) { productclass pc = new productclass(); pc.pr_name = name; pc.code = code; pc.category = category; pc.expiry = date; pc.price = price; pc.pr_image = img_name; dalinsert di = new dalinsert(); bool flag = di.insert(pc); }
correct way pass data ajax post via webmethod this.
var params = "{'name' :'" + nm + "', 'code' :'" + cde + "', 'category' :'" + cat + "', 'date' : '" + dt + ", 'price' :'" + pr + "' , 'img_name' :'" + f3 + "' }"
// declare above $.ajax...
data: params,
//use above in $.ajax...
.
Comments
Post a Comment