简体   繁体   English

如何使用Ajax从纯HTML调用远程服务器上托管的.asmx Web服务

[英]How to call .asmx web service hosted on Remote server from plain html using ajax

I've created a simple web service using asp.net and hosted on my machine's IIS server. 我使用asp.net创建了一个简单的Web服务,并将其托管在计算机的IIS服务器上。 I am trying to call this web service from plain html page without using Asp.net. 我试图不使用Asp.net从纯HTML页面调用此Web服务。 The problem is that I am not getting response. 问题是我没有得到回应。 Here is my code below : 这是我的代码如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Calling Classic Web Services with jQuery</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function () {
        $("a#SayHello").click(function (e) {
          e.preventDefault();

          $.ajax({
            type: 'POST',
            data: '{firstName:10,lastName:15}',
          //  url: 'Service.asmx/SayHello',
            url: 'http://192.168.1.20/MyService/Service.asmx/SayHello',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',

            success: function(response) {

                alert(response.d); //getting the Response from JSON

            },
            failure: function(msg) {
                alert(msg);
            }
          });
        });
      });    
    </script>
</head>
<body>
  <input id="name" /><a id="SayHello" href="#">Greetings!</a>  
</body>
</html>

Can anybody please help me where I am going wrong in this code..?? 有人可以帮我解决这段代码中的错误吗? If I run this in ASP.Net environment, it works perfectly. 如果我在ASP.Net环境中运行此程序,它将运行完美。 But if I host this, then it doesn't work. 但是,如果我主持此活动,那么它将无法正常工作。 Please help me...!! 请帮我...!!

Are you sure the web service is responding in JSON format? 您确定Web服务以JSON格式响应吗? It usually answers in XML. 它通常以XML回答。 Also your <script> block should go in the <body> and not in the <head> . 同样,您的<script>块应放在<body>而不是<head>

如果您已将Web服务托管在同一IIS上,则将192.168.1.20更改为localhost,然后尝试。

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

相关问题 如何使用纯HTML页面调用.asmx Web服务 - How to call .asmx web service using plain html page 如何ajax调用托管在远程服务器上的php文件并接收响应? - How to ajax call a php file hosted on remote server and receive a response? 使用ajax将数据发布到asmx网络服务并使用javascript函数在html页面中写入响应时出错 - Error while posting data to asmx web service using ajax and writing response in html page using a javascript function 如何从HTML访问asmx服务? - how to access asmx service from HTML? 尝试从Jquery $ .ajax调用输出HTML到Web服务 - Trying to output HTML from Jquery $.ajax call to web service 从html页面调用REST Web服务spring控制器,并使用ajax将表单元素与调用一起单独传递 - Calling a REST web service spring controller from a html page, and passing the form elements alone with the call using ajax 如何通过Asmx Web服务返回图像并在HTML页面中显示 - How to return an image through asmx web service and show in html page 如何从包含pdf附件的Web服务中调用ajax中的函数? - How to call a function in ajax from web service that contains pdf attachment? 如何在Web服务器上托管的index.html上从本地计算机流式传输声音 - How to stream sounds from local computer on my index.html which is hosted on a web server 无法从HTML页面调用Web服务 - cant call the web service from html page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM