简体   繁体   中英

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. I am trying to call this web service from plain html page without using Asp.net. 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. But if I host this, then it doesn't work. Please help me...!!

Are you sure the web service is responding in JSON format? It usually answers in XML. Also your <script> block should go in the <body> and not in the <head> .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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