0

我使用 (Jquery & PhoneGap) 创建了一个 android 应用程序。应用程序运行良好 现在,我想从我的 ASP.NET Web 服务中检索数据。

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 网络服务

/// <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";
        }
    }
4

3 回答 3

1

可能是这样的

$.ajax({
  url: 'yourPage/yourstatiCmethod',
  contentType: "application/json; charset=utf-8",
  dataType: "json"
  type: "POST",
  data:"{}"
  success: function(data) {
        alert('Do your all fetching Service');
  }
});
于 2012-04-20T06:52:48.443 回答
1

首先取消注释服务中的以下行

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

要调用 hello world 方法,请使用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 () {

  }
});

我会向我们推荐WCF REST服务

于 2012-04-20T06:53:12.080 回答
0

这可能会帮助您尝试此代码

$.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) {
    }
});
于 2016-02-26T10:46:09.807 回答