简体   繁体   English

如何从客户端JavaScript调用Web服务?

[英]How to call a webservice from client side JavaScript?

I want to call a web service of which URL is available to me. 我想呼叫一个URL可供我使用的Web服务。 I want to just call it and retrieve the result back using javascript Ajax. 我只想调用它,然后使用javascript Ajax取回结果。 For example : if some web service of like addition of two numbers is available freely and I want to use it in my app, how should I start ? 例如:如果某些网络服务如两个数字的加法免费提供,而我想在我的应用程序中使用它,该如何开始? I have just implemented following code (don't know it is right or not): 我刚刚实现了以下代码(不知道它是否正确):

function webServiceCallResult(){    
    var xmlHttpReq = getXMLHttpRequest();
      if(xmlHttpReq == null){
        alert('Exception occurred');
        return false;
    }    
    var strURL = "http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml";
    //var strURL = 'http://w3schools.com/dom/note.xml';
    if(xmlHttpReq.readyState == 0){
        xmlHttpReq.open('GET', strURL, true);
        xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlHttpReq.onreadystatechange = function() {
            if (xmlHttpReq.readyState == 4) {
                var resultString = xmlHttpReq.responseXML;              
                document.getElementById('webserviceresponsetext').value = resultString.getElementsByTagName("website")[0].childNodes[0].nodeValue;  
            }
        }
        xmlHttpReq.send();
    }
}

This is working fine in IE but giving error in FF, Opera etc.like 这在IE中工作正常,但在FF,Opera等中给出错误

XML parsing error,no element found Location: moz-nullprincipal:{ce91453b-f84c-4ce8-b02c-b999ef9f013a} Line Number 1, Column 1

Is it even possible to call a web service without using SOAP service request ? 甚至可以在不使用SOAP服务请求的情况下调用Web服务? Thanks.. 谢谢..

Unless you want to deal with the browser specific cross-compatibility stuff you should be using a library to handle Ajax requests. 除非您想处理浏览器特定的交叉兼容性问题,否则应该使用库来处理Ajax请求。

Also I saw you are making an Ajax request to a different server. 我还看到您正在向其他服务器发出Ajax请求。 Unfortunately the same origin policy prevents cross domain XHR, but there is a workaround about this, by using JSONP. 不幸的是,相同的原始策略阻止了跨域XHR,但是使用JSONP可以解决此问题。

I recommend using jQuery's Ajax method with JSONP, read some articles about this here: http://remysharp.com/2007/10/08/what-is-jsonp/ 我建议将JSON的jQuery Ajax方法与JSONP结合使用,请在此处阅读有关此内容的一些文章: http : //remysharp.com/2007/10/08/what-is-jsonp/
http://www.giantflyingsaucer.com/blog/?p=2682 http://www.giantflyingsaucer.com/blog/?p=2682

AFAIK the same origin policy prevents cross domain XHR. AFAIK相同的来源策略可防止跨域XHR。 You'll have to host a service on your server side which talks to the remote endpoint. 您必须在服务器端托管与远程端点对话的服务。

Are you firing the request from same domain? 您是从同一个域触发请求吗?

Firefox won't allow you to use XMLHttpRequest to access a different domain. Firefox不允许您使用XMLHttpRequest访问其他域。 See the link below. 请参阅下面的链接。

http://www.captain.at/howto-ajax-permission-denied-xmlhttprequest.php http://www.captain.at/howto-ajax-permission-denied-xmlhttprequest.php

You should call the request from the pages under http://www.androidpeople.com/ only. 您只能从http://www.androidpeople.com/下的页面调用请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM