Hi All,
Please tell me how i can call a web service from an HTML page using javascript which works fine on both IE and fire fox.I was successful in calling a ASP.NET web service using javascript in Internet Explorer (using service.useService method).Unfortunately the service.useService method is not working in fire fox. Please guid me to fix this.

Thanks

Dani AI

Generated

Three practical, realistic ways existed (and still do) to call a web service from a pure HTML page on another domain: JSONP, CORS, or a same-origin proxy. Each has clear tradeoffs—JSONP is simple but GET-only and unsafe if the provider is untrusted; CORS is the modern, full-featured solution but requires server-side headers; a server-side proxy is the most robust if the remote service cannot be changed.

JSONP (quick, if the service supports it)

  • JSON wrapped in a callback and loaded via a <script> tag. Works without special browser settings.
  • Limitation: only GET, and the returned payload is executed as script (security risk).
  • Minimal client example:
function jsonp(url, callbackName, cb) {
  window[callbackName] = function(data){
    try { cb(null, data); } finally {
      delete window[callbackName];
      document.head.removeChild(script);
    }
  };
  var script = document.createElement('script');
  script.src = url + (url.indexOf('?') === -1 ? '?' : '&') + 'callback=' + callbackName;
  document.head.appendChild(script);
}

CORS (recommended when server control is possible)

  • Server must set Access-Control-Allow-Origin (either a specific origin or '*'). For credentials, also set Access-Control-Allow-Credentials and return a matching origin.
  • Supports all HTTP methods and custom headers; note that non-simple requests trigger an OPTIONS preflight.
  • Client fetch example:
fetch('https://www.xyz.com/Services/MyService.asmx/Method', { mode: 'cors' })
  .then(r => r.ok ? r.json() : Promise.reject(r.statusText))
  .then(data => console.log(data))
  .catch(err => console.error(err));

Server-side proxy (when the remote service cannot be changed)

  • Proxy the remote call from a same-origin endpoint on the host page. Validate inputs, restrict destinations, and cache where appropriate.
  • Slightly more work but removes all browser cross-origin limits.

Notes tied to earlier replies: correctly listed proxy/Flash as historical options; ’s jQuery approach is useful because jQuery simplifies JSONP calls (set dataType: 'jsonp'). ’s note about solving it with JSON likely means JSONP — the pattern above shows how it’s done. For diagnosis, use browser devtools to inspect network requests and response headers (look for Access-Control-Allow-Origin).

Recommended Answers

All 8 Replies

Hi
Thanks for the reply. But i want some thing different. I will explain the scenario correctly.
I have an Asp.Net webservice in our domain "xyz", and the webservice can be accessed through http://www.xyz.com/Servecies/MyService.asmx. I want to call this webservice from a pure HTML page using javascript. This HTML page resides in another domain where we doesn't have any access to that HTML page. We have the right to provide only the Javascript functions or javascript files to call this web service. I got a cross domain issue while doing a test application. How can i fix this cross domain web service issue in both IE and Fire fox .. Please help me.

Thanks

You cant.

Not just using javascript.

With IE you can get your user to set there browser settings to low and it will let them do this but firefox or any other browser will not allow it as its a major security leak.

The best ways i have seen/and done this are the following 2 methods.

1) Create a web service page to give to your client/supplier which calls the webservice on a seperate domain. Then call the "same domain" web service in your javascript. The server side code wont have any restrictions on cross domain interaction.

2) Create a flash object which initialises XMLHTTP as flash doesnt have any objections to retrieving data across domains. Then when you have read all your info into your flash movie pass the data to your javascript in the same page.


Option 1 Seems to be a more elegant solution but if your page is flash heavy then maybe there is a possibility you could use this technique. There seems to be some movement towards creating a standard to allow cross domain interaction but i cant see it coming to light for javascript for a very long time if ever.

Hope this helps

Hi
Unfortunately we cant use these two suggestion since our requirement does not allow to do so. I was able to solve this issue using JSON. Thanks for the information..

I have the same problem. Could you please post your sollution using JSON?

Hi
Unfortunately we cant use these two suggestion since our requirement does not allow to do so. I was able to solve this issue using JSON. Thanks for the information..

Hi, I also have exactly the same requirements and I am also unable to call web service on Fire Fox. I have tried different methods but none have been successful. I would like to get some tips related to the solution which you have provided using JSON.

Hi, I also have exactly the same requirements and I am also unable to call web service on Fire Fox. I have tried different methods but none have been successful. I would like to get some tips related to the solution which you have provided using JSON.

I hope this helps:

You can also look at the ajax documentation of JQuery itself

Hi de buurman.

I appreciate your help. Have you noticed that the current thread is two years old? Please do not resurrect old threads and have a look at forum rules.

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.