Dot-Net

{’d’:’’} 在 asp.net webservice 響應中是什麼意思

  • April 27, 2013

我創建了一個簡單的 C# asp.net Web 服務函式,它返回一個字元串消息

,我使用 jquery ajax 從頁面呼叫它。

C#:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld() {
   return DateTime.Now.ToString();
}

JS:

   $(document).ready(function() {
   //alert("ready");
       $.ajax({
           type: "POST",
           contentType: "application/json; chatset=utf-8",
           url: "WebService2.asmx/HelloWorld",
           data: "{}",
           dataType: "json",
           success: function(msg) {
               //alert(msg); //doesnt works
               alert(msg.d);
           }
       });
   });

我的問題是為什麼 alert(msg);不起作用

這是一種安全強化機制。

從本質上講,它有助於防止攻擊者從受害者網站讀取 JavaScript 數組(以 Json 格式下載)的 CSRF 類型的攻擊。他們可以通過覆蓋 JavaScript 的 Array 類型來做到這一點。d導致返回的 Json 不是數組,從而使 Array 覆蓋對攻擊者無用。

請參閱這篇精彩的博文:http: //haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx

ASP.NET 和 WCF JSON 服務端點實際上將其 JSON 包裝在具有“d”屬性的對像中,以規避使用 JSON 時的潛在安全漏洞

Phil Haack 的文章:http ://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx

這是從 ASP.NET3.5 引入的。如果你想msg在 3.5 之前和之後的兩個框架中工作,試試這個小技巧。

var data = msg.hasOwnProperty("d") ? msg.d : msg;

禮貌 Dave Ward:再也不用擔心 ASP.NET AJAX 的 .d

引用自:https://stackoverflow.com/questions/7227427