0

I have two projects:

ASP.net web site project:

Default.aspx

     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type = "text/javascript">

   function DisplayMessageCall() {
    $.ajax({
        type: "POST",
        url: "http://localhost:24218/Service1.asmx/HelloWorld",
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccessCall,
        error: OnErrorCall
    });
}
function OnSuccessCall(response) {
    $('#<%=lblOutput.ClientID%>').html(response.d);
}

function OnErrorCall(response) {
    alert(response.status + " " + response.statusText);
}

 </script>

  <h2>Example 1: Call Webservice using JQuery AJax (Without Input)</h2>

<asp:Button ID="btnGetMsg" runat="server" Text="Click Me" OnClientClick="DisplayMessageCall();return false;" /><br />

<asp:Label ID="lblOutput" runat="server" Text=""></asp:Label>

And ASP Web Service Application:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloWorld()
    {
        return "Hello World";
    }
}

I added in my ASP web site the reference for my webService but when I click on the button I get the error message. When I enter the url

                  (http://localhost:24218/Service1.asmx/HelloWorld) 

in the browser I get the following result:

          <string xmlns="http://tempuri.org/">Hello World</string>

I use ASP.NET 4.5.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Nabila
  • 191
  • 4
  • 19
  • 1
    ASMX is a legacy technology, and should not be used for new development. WCF or ASP.NET Web API should be used for all new development of web service clients and servers. One hint: Microsoft has retired the [ASMX Forum](http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/threads) on MSDN. – John Saunders Jan 18 '14 at 19:34

1 Answers1

0

Looks like you've configured a GET method instead of a POST method. If you switch your $ajax call to:

   $.ajax({
      type: "GET",
      . . .

then it may work

goofballLogic
  • 37,883
  • 8
  • 44
  • 62
  • It doesn't work where do I configure the GET or the POST method? – Nabila Jan 18 '14 at 18:53
  • Is it normal that I get an XML result when I click on the URL: Hello World ? – Nabila Jan 18 '14 at 18:54
  • 1
    Yes that's normal. You would need [WebMethod] [ScriptMethod(UseHttpGet=true)] ( see http://stackoverflow.com/a/10752837/275501 ) – goofballLogic Jan 18 '14 at 21:51
  • I added [WebMethod][ScriptMethod(UseHttpGet=true)] and it work's just fine if I have my web service located in my web site, but when I call the webservice located in another webservices project it doesn't work. Did I miss something after adding the service reference?? – Nabila Jan 18 '14 at 23:14
  • You shouldn't need a web reference at all if you're calling it from javascript. What is the error you get when it's located in another web services project? – goofballLogic Jan 18 '14 at 23:39
  • responseText return an empty string!! – Nabila Jan 19 '14 at 00:44
  • perhaps this is a same-origin policy problem? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Same_origin_policy_for_JavaScript – goofballLogic Jan 19 '14 at 00:53
  • I have tried to launch my application in different browsers but still the same problem responseText return an empty string – Nabila Jan 19 '14 at 01:32
  • What is the URL of the page you are loading, and the URL of the web service? – goofballLogic Jan 19 '14 at 13:53
  • The url of the page is: http://localhost:22394/WebSite/Default.aspx The url of the web service is: http://localhost:29265/WebService.asmx/HelloWorld – Nabila Jan 19 '14 at 18:42
  • Yes, you can't go from one port (22394) to another (29265). See here: http://stackoverflow.com/questions/2099728/how-do-i-send-an-ajax-request-on-a-different-port-with-jquery – goofballLogic Jan 20 '14 at 11:25
  • Ok, another question please. Is there a way to run my two applications using the same port? – Nabila Jan 20 '14 at 15:36
  • not from visual studio as far as i know. Either configure them as virtual applications in IIS, or use JSONP – goofballLogic Jan 21 '14 at 10:15