0

I have created an android application using (Jquery & PhoneGap). The application works fine Now, I want to retrieve data from my ASP.NET Web Service.

html:

<html>
<head>
    <meta charset="utf-8">
    <title>My Friends</title>

    <link href="Jquery-mobile/jquery.mobile-1.1.0.css" rel="stylesheet" type="text/css" />
    <script src="Jquery-mobile/jquery-1.4.1.js" type="text/javascript"></script>

    <script src="Jquery-mobile/jquery.mobile-1.1.0.js" type="text/javascript"></script>
     <script type="text/javascript">


        $(function(){
             //code to fetch data from webservice  

             alert($("#test").html());
        });
    </script>

  </head>
  <body>
</body>
</html>

asp.net web service

/// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
2
  • Why don't use wcf service instead? I'ts more javascript friendly than a classic soap web service Commented Apr 20, 2012 at 6:43
  • i have no problem with wcf but how do i call it ? Commented Apr 20, 2012 at 6:53

3 Answers 3

1

First uncomment the following line in the service

    .....
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 

    [System.Web.Script.Services.ScriptService] 
    // uncomment this ^ ^
    public class WebService1 : System.Web.Services.WebService
    {
    .....

To call the hello world method use jQuery.ajax

$.ajax({
  type: "POST",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  url: "WebService1.asmx/HelloWorld",
  data: "{}",
  success: function(msg){
      $("body").append(msg.d); //will append "Hello world" to body tag
  },
  error: function () {

  }
});

I would recommend you to us WCF REST services instead

Sign up to request clarification or add additional context in comments.

Comments

1

may be some thing like this

$.ajax({
  url: 'yourPage/yourstatiCmethod',
  contentType: "application/json; charset=utf-8",
  dataType: "json"
  type: "POST",
  data:"{}"
  success: function(data) {
        alert('Do your all fetching Service');
  }
});

Comments

0

this may be help you try this code

$.ajax({
    type: "POST",
    url: URL,
    async: false,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    jsonp: "onJSONPLoad",
    jsonpCallback: "newarticlescallback",
    crossDomain: "false",
    beforeSend: function (xhr) {
    },
    error: function (request, status, error) {
        console.log("Error In Web Service...." + request.responseText);
        return false;
    },
    success: function (data) {
        Result = data;
    },
    complete: function (xhr, status) {
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.