0

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...!!

Ruchir
  • 1,086
  • 4
  • 24
  • 48
  • Have you tried everything from here? http://stackoverflow.com/questions/879362/calling-asmx-from-jquery also can you provide the relevant server-side code and class. Cheers. – Michael Coxon Nov 19 '14 at 11:27

2 Answers2

0

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>.

Nelson Pires
  • 321
  • 4
  • 12
0

if you have hosted the web service on the same IIS, then change 192.168.1.20 to localhost and then try..

Atul
  • 11
  • 1