【问题标题】:How to make AJAX call from jquery mobile to ASP.NET Webservice如何从 jquery mobile 向 ASP.NET Webservice 进行 AJAX 调用
【发布时间】:2012-04-20 06:38:30
【问题描述】:

我使用 (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";
        }
    }

【问题讨论】:

  • 为什么不使用 wcf 服务呢?我比经典的肥皂网络服务对 javascript 更友好
  • 我对 wcf 没有任何问题,但我该如何称呼它?

标签: jquery android asp.net jquery-mobile


【解决方案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 services

【讨论】:

    【解决方案2】:

    可能是这样的

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

    【讨论】:

      【解决方案3】:

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

      $.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) {
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-05
        • 2014-08-16
        • 2013-07-04
        • 1970-01-01
        相关资源
        最近更新 更多