0

I want to use c# web service (*.asmx) in php via jQuery.
my web servis is :
http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit
and my php code is :

    $.ajax({
      type: "POST",
      url: "http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit",
      dataType: 'text/xml; charset=utf-8',
      success: function(data) {alert('ok')},
      error  : function(e) {alert('error')}
    });

But it dont work.
i need help

1
  • 2
    Welcome to Stack Overflow! w3schools is a wrong and misleading site. You shouldn't use it as reference for any sort of language. For PHP, there's the PHP Manual, for JavaScript, there's Mozilla Developer Network (or MDN). See w3fools.com to further understand why you should never use w3schools. Commented Dec 8, 2012 at 16:39

1 Answer 1

1

You are violating the same origin policy restriction that's built into the browsers. You cannot send cross domain AJAX requests. If you want this to work the ASMX web service must be located on the same domain as the PHP page hosting this javascript code. In this example that would be http://www.w3schools.com/.

If the service you are attempting to consume is located on a remote domain you could write a PHP script on your domain which will consume the remote service (by sending the appropriate HTTP request) and then send an AJAX request to this PHP script instead of directly attempting to call the remote service which is not possible. The new PHP script will simply act as a bridge between your domain and the remote domain where the ASMX service is hosted.

Of course if the remote service supports JSONP or CORS you could consume it directly. That's not the case with ASMX services out-of-the box. Contact the author of the service to see if he supports them.

Sign up to request clarification or add additional context in comments.

1 Comment

CORS support in many browsers is still incomplete, it is nice one and I've used it as primary data exchange method but I also did make sure that there is JSONP fallback if CORS is not available or if it is broken in client browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.