使用 HTML 中的 Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15761044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:06:25  来源:igfitidea点击:

Consuming web service in HTML

javascripthtmlweb-services

提问by NewBee

I have created a web service and saved its .wsdl file. I want to consume this service in my HTML file by providing its URL.

我创建了一个 Web 服务并保存了它的 .wsdl 文件。我想通过提供它的 URL 在我的 HTML 文件中使用这个服务。

Can anybody tell me how to call the URL in HTML and use it? Like its done here. http://www.codeproject.com/Articles/14610/Calling-Web-Services-from-HTML-Pages-using-JavaScr/

谁能告诉我如何在 HTML 中调用 URL 并使用它?就像在这里完成的一样。 http://www.codeproject.com/Articles/14610/Calling-Web-Services-from-HTML-Pages-using-JavaScr/

The only difference is my web service does not have an extention like "asmx?wsdl". Does that makes any difference. I followed that tutorial too but it does not produce any output.

唯一的区别是我的 Web 服务没有像“asmx?wsdl”这样的扩展名。这有什么区别吗。我也遵循了该教程,但它没有产生任何输出。

Thank You////

谢谢你////

回答by thomas

You definitly should get familiar with AJAX. You could use the ajax functionalities provided by jQuery. That's the easiest way, I assume. Take a look at http://api.jquery.com/jQuery.ajax/

您绝对应该熟悉AJAX。您可以使用 jQuery 提供的 ajax 功能。这是最简单的方法,我想。看看http://api.jquery.com/jQuery.ajax/

You can use this like

你可以像这样使用

$.ajax({
  url: "urltoyourservice.xml"
  dataType: "xml",
}).done(function(data) {
  console.log(data);
});

HTML itself cannot consume a webservice. Javascript ist definitely needed. The existence of your WSDL File looks like you are probably using a XML format as the return of the web service. You have to deal with XML in javascript. Therefore take a look at Tim Downsexplanation.

HTML 本身不能使用网络服务。Javascript is 绝对需要。您的 WSDL 文件的存在看起来您可能正在使用 XML 格式作为 Web 服务的返回。您必须在 javascript 中处理 XML。因此,请看一下Tim Downs 的解释。

But keep in mind, that your web service URL must be available under the same domain as your consumption HTML-File. Otherwise you'll receive a cross-site-scripting error.

但请记住,您的 Web 服务 URL 必须与您的消费 HTML 文件位于同一域下。否则,您将收到跨站点脚本错误

回答by lngs

yes you can use ajax but keep in mind you wont be able to make a request across domains. Consuming web services should be done in server side.

是的,您可以使用 ajax,但请记住,您将无法跨域发出请求。使用 Web 服务应该在服务器端完成。

To further gain more knowledge about this, read How do I call a web service from javascript

要进一步了解这方面的知识,请阅读如何从 javascript 调用 Web 服务

回答by Dennis

function submit_form(){
        var username=$("#username").val();
        var password=$("#password").val(); 
                var data = { loginName: username, password:password };
                $.ajax( {
                    type: "POST",
                    url:"Paste ur service address here",
                    data: JSON.stringify(data),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: false,
                    success: function(data){
                        var value = data.d;
                        if(value.UserID != 0){
                            alert.html('success');
                        }
                    },
                    error: function (e) {
                    }
                });  
        }

Try this it will help

试试这个它会有所帮助