Hi All,
I am having issue in calling asp.net web service from javascript. The web service resides in one of our development machine and i am trying to call the web service from my local machine. (The application works fine if the webservice resides in my local machine) The reference for webservice is added to Scriptmanager. ScriptManager1.Services.Add(new ServiceReference(""); I debugged the code.. The scriptmanager takes the correct webservice reference. The client proxy files (jsdebug) are also generating correctly. But when i call the webservice it seems to take localhost as the root instead of the development machine ip (noticed using firebug). Please help me to solve this issue

Thanks

Dani AI

Generated

Brief diagnosis: this is not a ScriptManager bug but the browser same-origin policy and how the ASP.NET AJAX client proxy resolves service paths. The server-side build can show the external IP, and the generated debug JS can look correct in the page source, yet the runtime XHR will resolve to the page origin (localhost in a debug run). That explains 's observation that proxies are generated but Firebug shows requests to localhost.

Most reliable fix (same approach hinted at): put a thin server-side proxy/wrapper in the web app so all AJAX calls stay same-origin. The proxy forwards requests to the real .asmx and returns the response JSON to the browser. Example minimal handler:

<%@ WebHandler Language="C#" Class="ProxyHandler" %>

using System;
using System.Web;
using System.Net;
using System.IO;

public class ProxyHandler : IHttpHandler {
  public void ProcessRequest(HttpContext ctx) {
    var target = "http://xxx.xxx.xx.xx/MyService/Service.asmx";
    var req = (HttpWebRequest)WebRequest.Create(target);
    req.Method = "POST";
    using (var resp = (HttpWebResponse)req.GetResponse())
    using (var sr = new StreamReader(resp.GetResponseStream()))
      ctx.Response.Write(sr.ReadToEnd());
  }
  public bool IsReusable { get { return false; } }
}

Other options if control over the remote service exists: enable CORS on the service (add Access-Control-Allow-Origin and handle OPTIONS preflight), implement a JSONP-friendly wrapper, or run both site and service under the same host/port (use full IIS or a hosts-file mapping for local debugging). For CORS, a common minimal pattern is to add the Access-Control headers in Global.asax during BeginRequest (restrict origins in production).

Quick troubleshooting checklist: inspect the XHR URL and console for CORS errors (Origin header / preflight OPTIONS), confirm whether the generated client proxy uses a relative path at runtime, and test the proxy endpoint directly (curl or browser) to confirm forwarding. As noted, local debugging is easiest when the app and service share the same origin (IIS/host mapping) or when a local proxy is used.

Recommended Answers

All 4 Replies

What if:
1. you first create the follwing in the web.config file

<appSettings>
    <add key="servicePath" value=""/>
  </appSettings>

2. Then you add using System.Configuration; in the source file
3. in the page load method:

protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager1.Services.Add(new ServiceReference(ConfigurationManager.AppSettings["servicePath"].ToString()));
        }

Will this help?

/Frank

Hi Frank,
I am also doing the same procedure. While debugging i can see that the ScriptManager takes the correct webservice path as in the webconfig file. The scriptmanager also creates the client proxy files correctly. But once the page gets loaded in the browser the web service URL is changed back to localhost instead of the development machine URL ("") . I can also see that the URL is correctly redendered in the source file.

Thanks

Hi,
I was already setting the ScriptService enabled webservice path in web.config and adding a new scriptreference to the ScriptManager, just like Frank mentions.. When I build my website and then run I am able to get results. But my issue is I want to be able to debug as well at least in my development environment on my machine through ASP.Net solution. Hence when I try to debug I see in the AJAX generated code, when it is trying to invoke the result of this._get_path() has only virtual directory onwards (not the localhost/... ) and when I in the QuickWatch window try to reset it to be with the whole http://localhost/myvirtualdir... it gives me access denied when I try to execute right after resetting the path. It is a pain not to be able to debug from the client side. I have to have a test button to call the webservice on the server side to make sure I get the results and then put the call on the client side and build the website to see the results, that is what it is amounting to right now. Totally frustrating. Any help is appreciated.

... LotusShiv

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.