Asked by:
Unknown Web Method GetCategoriesById

Question
-
User-1499457942 posted
Hi
Below is the Code
function getbyID(Id) { $.ajax({ url: "/WebService.asmx/GetCategoriesById" + Id, type: "POST", contentType: "application/json;charset=UTF-8", dataType: "json", success: function (result) { $('#txtNo').val(result.No); $('#txtDescription').val(result.Description); $('#myModal').modal('show'); $('#btnUpdate').show(); }, error: function (errormessage) { alert(errormessage.responseText); } }); return false; } [WebMethod] public void GetCategoriesById(int Id) { con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; List<StoreCategory> objCategories = new List<StoreCategory>(); SqlCommand cmd = new SqlCommand("Sp_Categories", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Action", "O"); if (con.State == ConnectionState.Closed) { con.Open(); } SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { StoreCategory Category = new StoreCategory(); Category.No = rdr["No"].ToString(); Category.Description = rdr["Description"].ToString(); objCategories.Add(Category); } JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Write(js.Serialize(objCategories)); con.Close(); }
Thanks
Sunday, July 29, 2018 10:06 AM
All replies
-
User475983607 posted
You have a fundamental HTTP bug.
The is incorrect HTTP syntax.
url: "/WebService.asmx/GetCategoriesById" + Id
In HTTP parameters are passed in the URL like so.
url: "/WebService.asmx/GetCategoriesById?Id=" + Id
Secondly, you are not following standards. Parameters are submitted in the HTTP body when using an HTTP POST. HTTP GET submits parameters in the URL.
Return a type form the WebMethod just like any method. Do not do this.
JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Write(js.Serialize(objCategories));
Tested and verified HTTP GET and POST example based on your code.
ASMX
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Script.Services; using System.Web.Services; namespace WebFormsDemo { /// <summary> /// Summary description for WebService1 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] [ScriptMethod(UseHttpGet = true)] public int GetCategoriesById(int Id) { return Id; } [WebMethod] public int GetCategoriesById2(int Id) { return Id; } [WebMethod] [ScriptMethod(UseHttpGet = true)] public string HelloWorld() { return "Hello World"; } } }
Web Form
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebMethodClient.aspx.cs" Inherits="WebFormsDemo.WebMethodClient" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <input id="Button1" type="button" value="Get" /> <input id="Button2" type="button" value="Post" /> <div id="result"></div> </div> </form> <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script> <script> $(function () { $('#Button1').click(function (e) { e.preventDefault(); getbyID(100); }); $('#Button2').click(function (e) { e.preventDefault(); postbyID(500); }); function getbyID(Id) { $.ajax({ type: "GET", url: "/WebService1.asmx/GetCategoriesById?Id=" + Id, contentType: "application/json;charset=UTF-8", dataType: "json", success: function (result) { $('#result').text(result.d); console.log(result.d); }, error: function (errormessage) { alert(errormessage.responseText); } }); return false; } function postbyID(Id) { data = {}; data.Id = Id $.ajax({ type: "POST", url: "/WebService1.asmx/GetCategoriesById2", data: JSON.stringify(data), contentType: "application/json;charset=UTF-8", dataType: "json", success: function (result) { $('#result').text(result.d); console.log(result.d); }, error: function (errormessage) { alert(errormessage.responseText); } }); return false; } }); </script> </body> </html>
Sunday, July 29, 2018 12:10 PM -
User-1171043462 posted
You are doing POST and hence you don;t need to pass parameter in QueryString
function getbyID(Id) { $.ajax({ url: "/WebService.asmx/GetCategoriesById", data: "{Id: " + Id + "}", type: "POST", contentType: "application/json;charset=UTF-8", dataType: "json", success: function (result) { $('#txtNo').val(result.No); $('#txtDescription').val(result.Description); $('#myModal').modal('show'); $('#btnUpdate').show(); }, error: function (errormessage) { alert(errormessage.responseText); } }); return false; }
Sunday, July 29, 2018 12:37 PM -
User-1171043462 posted
And You cannot use Response.Write in WebMethod, you need to return data
WebMethod] public string GetCategoriesById(int Id) { con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; List<StoreCategory> objCategories = new List<StoreCategory>(); SqlCommand cmd = new SqlCommand("Sp_Categories", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Action", "O"); if (con.State == ConnectionState.Closed) { con.Open(); } SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { StoreCategory Category = new StoreCategory(); Category.No = rdr["No"].ToString(); Category.Description = rdr["Description"].ToString(); objCategories.Add(Category); } con.Close(); JavaScriptSerializer js = new JavaScriptSerializer(); return js.Serialize(objCategories); }
JS:
function getbyID(Id) { $.ajax({ url: "/WebService.asmx/GetCategoriesById", data: "{Id: " + Id + "}", type: "POST", contentType: "application/json;charset=UTF-8", dataType: "json", success: function (r) { var result = eval(r); $('#txtNo').val(result.No); $('#txtDescription').val(result.Description); $('#myModal').modal('show'); $('#btnUpdate').show(); }, error: function (errormessage) { alert(errormessage.responseText); } }); return false; }
Sunday, July 29, 2018 12:40 PM