Search code examples
javascriptjqueryhtmlajaxwebmethod

AJAX Http POST : No transport error


This is my webmethod:

[WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

And this is the AJAX code in html page:

jQuery.support.cors = true;
                var btn_startOp = document.getElementById("startOpp");

                $.ajax({
                    type: "POST",
                    contentType: "application/xml; charset=utf-8",
                    data: "{}",  
                    dataType: "xml",
                    url: "http://localhost:61457/WebSite1/Service.asmx/HelloWorld",
                    success: function(msg){ alert(msg.d); },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(errorThrown);
                          }
                });

This is giving me an error : On,

  • IE Browser : No Transport
  • Mozilla : Unknown

Webservice is working fine when tested on browser.

UPDATE: This is the line in head tag in Html:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

Can anyone help me with this? This is the very first code for me..I searched a lot still its a mystery for me ... :) :(


Solution

  • Change to this:

    $.ajax({
      type: "GET",
      url: "/WebSite1/Service.asmx/HelloWorld",
      dataType:"json",
      success: function(msg) {
        alert(msg);
      },
      error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert(errorThrown);
      }
    });
    

    1. You are returning a string value from webmethod so dataType:"xml" isn't required.
    2. Webservice is working fine when tested on browser., It means you need to have a GET request.