Click here to Skip to main content
14,391,319 members
Rate this:
Please Sign up or sign in to vote.
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

[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...
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100