Saturday 3 December 2016

500 Error in Web Service or WebMethod in ASP.net or Asmx calling from Ajax or Js Call.

I got  One issues 500 Error  In Web Service in Asp.net ,I tried different Ways ,I am unable to find exact reason why ? That Reason I writing this Post .

In Browser Developer tools we find following error 500 Internal Server Error


Check the Following steps :-

Step 1:- 

    [System.Web.Script.Services.ScriptService]
    public class firstService : System.Web.Services.WebService
    {

     }

Check that Attribute is Given Above Service or Not .

if not you will get Error. Normally if we create the .asmx File by Default this attribute will be in comment mode ,please  un-comment .

Still issues not Resolved? following next Step 

Step 2:- 


        [ScriptMethod(UseHttpGet = false,ResponseFormat = ResponseFormat.Json)]
        [WebMethod]
        public  static List<employee>  Employee()
        {

          }


Check your placed the following  attribute or not .

[ScriptMethod(UseHttpGet = false,ResponseFormat = ResponseFormat.Json)]

Bydefault  UseHttpGet is false ,you can write like 

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]


Still is Not Resolved ?

Step 3:-  Check the ajax code  what we write  there 


 $.ajax({
            type: "GET", ---->  Type of Call   // 
            url: "/webServices/firstService.asmx/Employee",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (list) {
                $("#Something").append("<ul id='bullets'></ul>");
                for (i = 0; i < list.d.length; i++) {
                    $("#bullets").append("<li>" + list.d[i].name + ":" + list.d[i].age);
                    
                }
            },
            error: function (e) {
                $("#Something").html("There was an error retrieving records");
            }
        });

We Need to Change the Type of Ajax Call.

  type: "GET", ----> // Issue here we need to Change the following line to " POST"

like  this


 $.ajax({
            type: "POST",
            url: "/webServices/firstService.asmx/Employee",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (list) {
                $("#Something").append("<ul id='bullets'></ul>");
                for (i = 0; i < list.d.length; i++) {
                    $("#bullets").append("<li>" + list.d[i].name + ":" + list.d[i].age);
                    
                }
            },
            error: function (e) {
                $("#Something").html("There was an error retrieving records");
            }
        });


NOW issue is Resolved You can able to call [WebMethod] of Service.