乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      c# – 無法從.net中的JSON響應(yīng)中刪除.d封裝

       印度阿三17 2019-07-15

      即使在Never worry about ASP.NET AJAX’s .d again這篇偉大的博客文章之后,我也無法逃避我的JSON響應(yīng)中的.d封裝.我不知道我是否做錯(cuò)了所以我會(huì)復(fù)制我的服務(wù)器端和客戶端代碼.

      我正在使用Newtonsoft.JSON庫序列化JSON.

      客戶端:

      <script type="text/javascript">
      $(function () {
          $("#bt").click(function () {
              $.ajax({
                  type: "POST",
                  url: "<%= Page.ResolveUrl("~/MapView.aspx/GetLocations")%>",
                  data: "{ type: '<%= Page.RouteData.Values["type"].ToString() %>', id: '<%= Page.RouteData.Values["id"].ToString() %>' }",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  dataFilter: function(data) {
                      var msg;
      
                      if (typeof (JSON) !== 'undefined' && 
                          typeof (JSON.parse) === 'function')
                          msg = JSON.parse(data);
                      else
                          msg = eval('('   data   ')');
      
                      if (msg.hasOwnProperty('d'))
                          return msg.d;
                      else
                          return msg;
                  },
                  success: function (msg) {
                      console.log(msg);
                  },
              });
          });
      });
      </script>
      

      服務(wù)器端

          [System.Web.Services.WebMethod]
          public static string GetLocations(int id, string type)
          {
              string data = "";
      
      
              if (type == "attraction")
              {
                  var attractions = db.Attraction
                                      .Where(a => a.AttractionId == id)
                                      .Select(p => new { p.AttractionId, p.Name, p.Description, p.Location })
                                      .ToList();
      
      
                  JObject attractionsJSON = new JObject(
                      new JProperty("Attractions", new JArray(
                          from a in attractions
                          select new JObject(
                              new JProperty("id", a.AttractionId),
                              new JProperty("name", a.Name),
                              new JProperty("location", a.Location),
                              new JProperty("description", a.Description)
                              ))));
      
                  data = attractionsJSON.ToString();
              }
              else if (type == "category")
              {
                  var attractions = db.Attraction
      
                      .Select(p => new { p.AttractionId, p.Name, p.Description, p.Location, p.CategoryId })
                      .ToList();
      
                  if (id != 0)
                  {
                      attractions = attractions.Where(a => a.CategoryId == id)
                          .ToList();
                  }
      
      
                  JObject attractionsJSON = new JObject(
                      new JProperty("Attractions", new JArray(
                          from a in attractions
                          select new JObject(
                              new JProperty("id", a.AttractionId),
                              new JProperty("name", a.Name),
                              new JProperty("location", a.Location),
                              new JProperty("description", a.Description)
                              ))));
      
                  data = attractionsJSON.ToString();
      
              }
              return data;
          }
      

      Serverside – 更新1

          [System.Web.Services.WebMethod]
          public static void GetLocations(int id, string type)
          {
              string data = "";
      
      
              if (type == "attraction")
              {
                  var attractions = db.Attraction
                                      .Where(a => a.AttractionId == id)
                                      .Select(p => new { p.AttractionId, p.Name, p.Description, p.Location })
                                      .ToList();
      
      
                  JArray attractionsJSON = new JArray(
                          from a in attractions
                          select new JObject(
                              new JProperty("id", a.AttractionId),
                              new JProperty("name", a.Name),
                              new JProperty("location", a.Location),
                              new JProperty("description", a.Description)
                              ));
      
                  data = attractionsJSON.ToString();
              }
              else if (type == "category")
              {
                  var attractions = db.Attraction
      
                      .Select(p => new { p.AttractionId, p.Name, p.Description, p.Location, p.CategoryId })
                      .ToList();
      
                  if (id != 0)
                  {
                      attractions = attractions.Where(a => a.CategoryId == id)
                          .ToList();
                  }
      
      
                  JArray attractionsJSON = new JArray(
                          from a in attractions
                          select new JObject(
                              new JProperty("id", a.AttractionId),
                              new JProperty("name", a.Name),
                              new JProperty("location", a.Location),
                              new JProperty("description", a.Description)
                              ));
      
                  data = attractionsJSON.ToString();
      
              }
      
      
              var js = new System.Web.Script.Serialization.JavaScriptSerializer();
      
              HttpContext.Current.Response.Clear();
              HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
              HttpContext.Current.Response.Write(js.Serialize(data));
              HttpContext.Current.Response.Flush();
              HttpContext.Current.Response.End();
          }
      }
      

      解決方法:

      嘗試使用void方法,不返回.而是自己寫輸出來回應(yīng)!

        [System.Web.Services.WebMethod]
          public static void GetLocations(int id, string type)
          {
           // var attractions = something ;
      
           var js = new System.Web.Script.Serialization.JavaScriptSerializer();
      
              HttpContext.Current.Response.Clear();
              HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
              HttpContext.Current.Response.Write(  js.Serialize(attractions )  );
              HttpContext.Current.Response.Flush();
              HttpContext.Current.Response.End();
         }
      

      保持這兩個(gè)電話結(jié)束非常重要:

      flush:確保將輸出寫入流

      結(jié)束:結(jié)束流以防止asp.Net將空J(rèn)son寫入流

      來源:https://www./content-1-328801.html

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多