Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to access data from database using jquery. I have written following code in my script tag.

C#
<script type="text/javascript"  language="javascript">
     $(document).ready(function () {
         //function search(){
         $.ajax({
             type: "GET",
             url: location.pathname +"Home/AllCities",
             data: "{}",
             dataType: "json",
             contentType: "application/json; charset=utf-8",
             success: OnSuccess,
             error: onerror

         });
     });
     function onerror(data) {
         alert("error");
         
         var list = "<select>";
         for (var i = 0; i < data.d.length; i++) {
             list += " <option>" + data.d[i].CityName + "</option>";
         }
         list += "</select>";
         $("#displayDdl").append(list);
     }
     function OnSuccess(data) {
         alert("in success function");
         var list = "<select>" ;
         for (var i = 0; i < data.d.length; i++ ) {
             list += " <option>" + data.d[i].CityName + "</option>";
         }
         list += "</select>"; 
         $("#displayDdl").append(list);
     }
    
 
 </script>

I have included all necessary files to imlement jquery.
whatever path i write in the url following location.pathname it always gives 404 error.Can anybody suggest why it is happening?
Posted
Comments
Andy Lanng 17-Mar-15 13:12pm    
this looks odd:
url: location.pathname +"Home/AllCities",
are you using aliasing? otherwise I would expect to see an extension
Dolly Nimavat 17-Mar-15 13:16pm    
I am using MVC structure. AllCities is the web method written in HomeController
Andy Lanng 17-Mar-15 13:22pm    
Ah - Then I may be less useful to you then.
I did a quick giyf search and found that the url syntax should be something like url: "/Home/AllCities", whereas your url would be "<page>.aspxHome/AllCities"?
maybe I'm wrong. still need to learn mvc
Dolly Nimavat 17-Mar-15 13:50pm    
in mvc "Home/Allcities" means - localhost:1040/Home/AllCities
where Home is controller(Class inherited from Controller class) and Allcities is the method of home controller.
Andy Lanng 17-Mar-15 14:09pm    
That needs to be "location.hostname". "Location.pathname" gives you the path and name of the executing page. I can't imagine that being different in Mvc. Try a alert(location.pathname) to prove me wrong ^_^
You will still need the extra '\' before "home"

1 solution

More on this here[^]

You need location.hostname to get the root url. You're using location.pagename which includes the path and page executing the JavaScript.

These properties do not append a slash so you will need to prepend that to the string.

You can debug JavaScript in chrome and in Firefox using firebug. There are other debuggers out there but I don't rate them.

Failing that, you can always add an alert(string) to your code so you can see exactly what a property returns

Hope that helps ^_^
Don't forget to rate an subscribe ;P
 
Share this answer
 
v3

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