Click here to Skip to main content
15,881,826 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am fetching data from database and want to fill it in the dropdownlist. Following is my Javascript code .
<pre lang="c#">
 $(document).ready(function () {
         //function search(){
         alert(location.pathname);
         $.ajax({

             type: "GET",
             url:location.pathname+ "SearchFromHome.asmx/AllCities",
             data: "{}",
             dataType: "json",
             contentType: "application/json; charset=utf-8",
             success: OnSuccess,
             error: onerror

         });
     });

Following is the code in web service.
<pre lang="c#">
   [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json]
        public  List<City> AllCities()
        {
            List<City> cityList = new List<City>();

            using (cab_4_21Entities1 d = new cab_4_21Entities1())
            {
                cityList = d.Cities.ToList<City>();
            }
            return cityList;
        }

when I run the project, it gives 500 internal server error. In inspect element, console tab, the error location is shown in jquery file - jquery-2.1.3.min.js.
Posted

1 solution

Change the code as follows:
C#
$(document).ready(function () {
           $.ajax({
               type: "POST",
               url: "SearchFromHome.asmx/AllCities",
               data: "{}",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: ....,
               error:....
           });
       });

C#
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<City> AllCities()
{
  ...........
  ...........
}

Make sure to uncomment [System.Web.Script.Services.ScriptService] in your webservice file. I've tested this code and it's working.

Regards...
 
Share this answer
 
v3
Comments
Dolly Nimavat 20-Mar-15 7:18am    
Still the same error is there.
Dolly Nimavat 20-Mar-15 7:22am    
Can you please me why it shows the location of the error in jquery file?
Thanks7872 20-Mar-15 7:56am    
See the updated solution. Observe the changes.
Dolly Nimavat 20-Mar-15 8:37am    
I have updated my code as per your suggestions. Still the same error is there.

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