3

I want to call web services from my javascript. I've read a bunch of the posts on here such as

Simplest SOAP example

But the example post to webservicex.net returns null (xmlhttp.responseXML in firebug). Where i get confused is that all these libs/proxies/apps/etc talk about communicating with a web service but I don't see js or it appears everything is done in the backend.

I want to call a web service and have everything returned to js, the way that seems most rational is the use a proxy server to make the request so the browser doesn't complain.

I'm new to the web server world, Apache mod_proxy and/or a proxy server are pretty ambiguous to me, The docs said what they did but not implement.. Can someone provide some links to put me in the right direction with some examples or tutorials?

Thanks

Community
  • 1
  • 1
Crushing
  • 487
  • 1
  • 9
  • 24

4 Answers4

1

Depending on the API, it is possible to dynamically insert a <script> tag into your document like:

<script src="http://www.otherdomain.com/list.php?category=23&order=2"></script>

Then the script can send back the results in a JS like:

yourCallback([{'Stool', 12000}, {'Table', 4000}]);

And of course the yourCallback() function you have written will populate the HTML document with the results.

Of course it depends on the API you are using. If it is apublic API, it's interface is documented and this stuff is hidden. If it is not a public API, you probably are not OK legally to access their services from a third-party site.

vbence
  • 20,084
  • 9
  • 69
  • 118
  • I'm going to be writing the web service a bit later on a different machine located elsewhere, I think it's pushing the technology but I want to talk directly to the service, not through another script; I thought I read that mozilla supports script signatures to trust the script but that'll limit the platforms... – Crushing Mar 31 '11 at 19:04
  • This is not PHP-centric. You can use this model with your custom service. Your service can listen on a port, accept HTTP queries and generate JSON response. It is cross-domain safe. – vbence Mar 31 '11 at 19:48
1

If at all possible, you might want to try and use JSONP to do cross-domain AJAX requests. A quick breakdown of how it compares to normal JSON requests can be found here.

1

You can write simple PHP (or anything you prefer) script which takes URL and POST/GET params, feeds them to remote server and prints out result for your javascript.

So, you can call non-remote script to retrieve remote content.

Im0rtality
  • 3,463
  • 3
  • 31
  • 41
0

jsonp only support get, so I can not submitted data, the best way is to implement a cross-domain proxy in you the same domain. Reference here : http://www.codeproject.com/Articles/25218/Fast-Scalable-Streaming-AJAX-Proxy-continuously-de

Justin wong
  • 638
  • 6
  • 15