jquery - ajax call in ascx File -


i have ascx file, in making ajax call function located in file(aspx code behind file). returning complete aspx page in result, returning string in function,below code in ascx file

  $.ajax({             type: "post",             url: "myfile.aspx/getdata", //url point webmethod                       success: function (result) {                 alert('success');                 $("#txtlicense").val(result);             },             error: function () { alert('error'); }         }); 

and in myfile.aspx.cs

 [system.web.services.webmethod()]         public static string getdata()         { //getting data db , returning           } 

i tried placing method in ascx.cs file giving error

this type of page not served 

you missing

contenttype: "application/json; charset=utf-8", datatype: "json", 

see following working example

// code behind method declared static

[webmethod] public static string getsquare(string value) {     return value; } 

your button click has done

<input type="button" id="button" value="chnageurl" onclick="ajaxcall()" /> 

script this

<script type="text/jscript">  function ajaxcall(e) {              $.ajax({             type: "post",             url: "default.aspx/getsquare",             contenttype: "application/json; charset=utf-8",             data: json.stringify({ value: "vinay" }),             datatype: "json",             success: function (value) {             alert(value.d);         },        error: function () { alert("ajax error"); }      });   }; 


Comments