Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My webservice is not getting called. I am getting 500 internal server error. what should i do?

This is the HTML coding:

HTML
$(window).load(function () {
<pre>              $.ajax({
                        type: "GET",
                        data: {},
                        url:  "services/VisiTracWS.asmx/GetLocation",
                        success: function (response) {
                            alert("1");
                            var strSection = '<option value="0">Select Location</option>';
                            if (response.d.length > 0) {
                                for (i = 0; i < response.d.length; i++) {
                                    strSection = strSection + '<option value="' + response.d[i].LocationID + '">' + response.d[i].LocationName + '</option>';
                                }
                            }
                            $('#ddlLocations').html(strSection);
                        },
                        error: function (response) {
                            alert(response.status + " " + response.statusText);
                        }
                    });
                });



This is the webservice:

C#
<pre>[WebMethod]
        public static List<Location> GetLocation()
        {
            List<Location> lst = new List<Location>();
            try
            {
                visitrac.Location lc = new visitrac.Location();
                DataTable DT = lc.GetDropdown();

                lst = (from DataRow dr in DT.Rows
                       select new Location()
                       {
                           LocationID = Convert.ToInt32(dr["LocationID"]),
                           LocationName = Convert.ToString(dr["LocationName"]),
                       }).ToList();
            }
            catch (Exception ex)
            {
                // clsException.WriteLog("", "GetLocation", ex.Message);
            }
            return lst;
        }


What I have tried:

I have tried making the method static. changing POST to GET
Posted
Updated 7-Aug-17 23:42pm
v2
Comments
Richard MacCutchan 8-Aug-17 3:40am    
What you should do is look at the server logs to find out why it gave the 500 error.
prapti.n3 8-Aug-17 3:59am    
where do I find server logs in Mozilla firefox??
Richard MacCutchan 8-Aug-17 4:22am    
Look on the server.
Karthik_Mahalingam 8-Aug-17 4:03am    
is the url accessible from browser address bar ?
prapti.n3 8-Aug-17 4:40am    
yes.. i can access the web service

1 solution

try like this

Web Service c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebApplication135.services
{
    
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)] 
    [System.Web.Script.Services.ScriptService]
    public class VisiTracWS : System.Web.Services.WebService
    {

        [WebMethod]
        public   Location[] GetLocation()
        {
            List<Location> lst = new List<Location>();
            lst.Add(new Location() { LocationID = 1, LocationName = "India" });
            lst.Add(new Location() { LocationID = 2, LocationName = "Australia" });
            lst.Add(new Location() { LocationID = 3, LocationName = "China" });
            return lst.ToArray();
        }
    }

    public class Location {
        public int LocationID { get; set; }
        public string LocationName { get; set; }

    }
}


HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="Scripts/jquery-1.7.1.js"></script>
    <script>
        $(window).load(function () {
            $.ajax({
                type: "post",
                data: {},
                url: "services/VisiTracWS.asmx/GetLocation",
                contentType: "application/json; charset=utf-8", 
                dataType: "json",  
                success: function (response) { 
                    var strSection = '<option value="0">Select Location</option>';
                    if (response.d.length > 0) {
                        for (i = 0; i < response.d.length; i++) {
                            strSection = strSection + '<option value="' + response.d[i].LocationID + '">' + response.d[i].LocationName + '</option>';
                        }
                    }
                    $('#ddlLocations').html(strSection);
                },
                error: function (response) {
                    alert(response.status + " " + response.statusText);
                }
            });
        });
    </script>
</head>
<body>
    <select id="ddlLocations"></select>
</body>
</html>
 
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