0

I need the following jQuery call to work from the localhost as well as the production server, but can't figure out how to do this. This is in a master page, and calls can come from various places in the file structure as well. I would certainly appreciate any help.

        function getInternetLeadsCount() {
            $.ajax({
                type: "POST",
                url: "http://localhost:64558/mysite.com/Web_Services/AjaxWebService.asmx/HelloWorld",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: "{}",
                success: AjaxSucceeded,
                error: AjaxError
            });
        }

        function AjaxSucceeded(data, status) {
            alert('jQuery: ' + data.d);
        }

        function AjaxError(x, y, z) {
            alert(x + ' ' + y + ' ' + z);
        }
Keith Myers
  • 1,379
  • 2
  • 15
  • 29

4 Answers4

0

Use an absolute url:

url: "/Web_Services/AjaxWebService.asmx/HelloWorld"

ScottE
  • 21,530
  • 18
  • 94
  • 131
0
you can make webmethod in your aspx page and call that method like this.

$.ajax({
  type: "POST",
  url: "defaul.aspx/HelloWorld",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
alert(msg.d);
  }
});

OR you can have the absoulte path

$.ajax({
  type: "POST",
  contentType: "application/json; charset=utf-8",
  url: "WebService.asmx/HelloWorld",
  data: "{}",
  dataType: "json"
});
Ashfaq Shaikh
  • 1,648
  • 12
  • 20
  • I'd have to create the web method in each of the pages that needs this, since it's in a master page, which is quite a few. – Keith Myers Dec 22 '11 at 15:29
  • you can put $.ajax function in every page or suppose can have the server variable name it pagename. Public string _Pagename{get;set;} put this in master page and set it on every page. now you can make URL like this. url: "'+ <%=_Pagename%> +'" – Ashfaq Shaikh Dec 22 '11 at 15:33
0

First off, I would suggest that you install IIS or IIS Express on your workstation instead of working off of the built-in Visual Studio server. You should always to to develop in an identical environment to your production server. Doing otherwise can cause issues, as you have noticed.

Apart from that, I would probably use C# to render out a JavaScript variable containing the true web root. In your master page or aspx file:

<script type="text/javascript">
    var webRoot = '<%= Page.ResolveUrl("~/") %>';
    console.log(webRoot); // '/mysite.com'
</script>

On your workstation, webRoot='/mysite.com' and on production webRoot='/'. Then you can use the webRoot JavaScript variable to build up an application-relative path to your web service.

<script type="text/javascript">
    var url = webRoot + '/Web_Services/AjaxWebService.asmx/HelloWorld'
    console.log(url); // '/mysite.com/Web_Services/AjaxWebService.asmx/HelloWorld'
</script>
Jeff
  • 13,943
  • 11
  • 55
  • 103
0

Here's what worked, which came from Get URL of ASP.Net Page in code-behind

$.ajax({
                    type: "POST",
                    url: "<%= new Uri(Request.Url,Request.ApplicationPath) %>" + "/Web_Services/AjaxWebService.asmx/HelloWorld",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: "{}",
                    success: AjaxSucceeded
                });


            function AjaxSucceeded(data, status) {
                // Do whatever...
            }
Community
  • 1
  • 1
Keith Myers
  • 1,379
  • 2
  • 15
  • 29