0

the webservice looks like

using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using System.Web;<br/>
using System.Web.Services;<br/>
[WebService(Namespace = "http://tempuri.org/")]<br/>
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]<br/>
 [System.Web.Script.Services.ScriptService] <br/>
public class Map : System.Web.Services.WebService
{
   [WebMethod]
    public MapClass LatitudeLongitude()
    {
       MapClass mapobj = new MapClass();
       return mapobj;

    }
     [WebMethod]
  public string helloToYou() 
  { 

      return "Hello"; 
 } 

}


the class look like:

public class MapClass <br/>
{

    string latitude;

    string longitude;

    public MapClass()

    {

        latitude = "51.508742";
        longitude = "-0.120850";
    }

}

And i am trying to call the webservice from a html5 page

`</head>`<br/>
`<body>`<br/>

`<form id="form1" runat="server">`<br/>

`<h1>Map</h1>`<br/>

`<input id="View" type="button" value="Click to view" onclick="getData()"/>`<br/>

`</form>`<br/>

`</body>`<br/>


`<script type="text/javascript" src="D:\PepsiCO\jQuery\jquery-1.7.1.min.js"> </script>`<br/>

`<script type="text/javascript">`

    function getData() {

       $.ajax({

          type:"POST",
          url: "http://localhost:53788/HTML5_WebService_Maps/Map.asmx/helloToYou",
          contentType: "application/json;charset=utf-8",
          dataType: "json",
            complete: function (response)
            { alert('complete'); },
            success: function (response)
           { alert('Success'); },
            error: function (response)
            { alert('Failure'); }
        });
    }
`</script>`
`</html>`

when i browse the html page in chrome, it displays failure in alert box. Dont know why it isnt showing success. Please help.

Ravi Gadag
  • 15,331
  • 5
  • 53
  • 83
Libin
  • 33
  • 6
  • try adding `data: '{}',` to your ajax function. Not sure if it will work though. – thunderbird May 21 '13 at 09:28
  • 1
    make sure your server returning content type `application/json` – muneebShabbir May 21 '13 at 09:28
  • also set url to `url: "Map.asmx/helloToYou",` – thunderbird May 21 '13 at 09:30
  • @munnebShabbir how to check whether the server is returning content type 'application/json' – Libin May 21 '13 at 09:49
  • What's the error message? – Rune FS May 21 '13 at 10:18
  • @user2404699 type this in your web browser `http://localhost:53788/Map.asmx` and check if your webservice can be called from an ajax function. It will show you a page with all the `webmethods` if your webservice is accessible and correctly defined. – thunderbird May 21 '13 at 10:23
  • @Rune FS I tried to load the html page in IE. The status text is No response. status is 0. response text is undefined – Libin May 21 '13 at 10:37
  • @thunderbird i tried to access http://localhost:53788/HTML5_WebService_Maps/Map.asmx. I get a page with all accessible webmethods. http://localhost:53788/Map.asmx throws 404 error. how to check whether the webservice can be called from ajax function? – Libin May 21 '13 at 10:41
  • @user2404699 go to your `.asmx` markup page(rightclick the page in the solution explorer and select view markup) and add `~/` to the `codebehind` attribute(prefix it). you can access the webservice by setting your `url` to `"HTML5_WebService_Maps/Map.asmx/helloToYou"` – thunderbird May 21 '13 at 10:47
  • I hope this is what you meant @thunderbird – Libin May 21 '13 at 10:51
  • @user2404699 yes. are you using webforms or mvc? – thunderbird May 21 '13 at 10:53
  • @user2404699 shouldn't the class attribute be defined like this `projectname.Map` – thunderbird May 21 '13 at 11:02

3 Answers3

0

Try appending this to the webmethod:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

And return object with serializer:

JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(mapobj);
return strJSON;

Also it might be a good idea to post the error response:

error: ajaxFailed

function ajaxFailed(xmlRequest) {
$('#errordiv).html(xmlRequest.status + ' <br /> ' +
            xmlRequest.statusText + '<br />' +
            xmlRequest.responseText);
}

In response to your comment on this answer: Quoting https://stackoverflow.com/users/158502/langdon 'In my experience, you'll see a status of 0 when either doing cross-site scripting (where access is denied) or requesting a URL that is unreachable (typo, DNS issues, etc).'

jQuery Ajax - Status Code 0?

Community
  • 1
  • 1
Yeronimo
  • 1,625
  • 13
  • 22
  • 1
    You don't have to serialize data that has to be returned. asp.net does it automatically for you. – thunderbird May 21 '13 at 09:25
  • status is **0**; statusText is**error**; responsetext is **blank** – Libin May 21 '13 at 10:00
  • In my experience, you'll see a status of 0 when either doing cross-site scripting (where access is denied) or requesting a URL that is unreachable (typo, DNS issues, etc). Taken from: http://stackoverflow.com/questions/2000609/jquery-ajax-status-code-0 – Yeronimo May 21 '13 at 10:13
  • I tried to load the html page in IE. The status text is **No response**. status is **0**. response text is **undefined** – Libin May 21 '13 at 10:35
  • 1
    If you host the webservice on IIS in different app (so diff ports) than your html5 page, you are making cross-domain request, which gives problems like described in the linked question. What if you deploy your html5 page to the same app? – Yeronimo May 21 '13 at 10:38
  • the html page from which i am trying to access the web service is in different application. – Libin May 21 '13 at 10:43
  • 1
    So that is your problem then. See this for possible answer: http://stackoverflow.com/a/10618640/2215083 – Yeronimo May 21 '13 at 10:48
  • @Yeronimo I got the data from webservice when i browse in IE. But chrome still gives error status – Libin May 21 '13 at 11:22
0

change dataType to htmland check

dataType: "html",
Anjith K P
  • 2,100
  • 24
  • 34
0

Is your web page running on the same domain / port ? What is the status code of the response ?

mguimard
  • 1,831
  • 12
  • 14