簡體   English   中英

調用ASP.net(不是MVC)網絡方法並獲取json響應,因此我可以使用foreach(d3.js)遍歷列表嗎?

[英]call an ASP.net (not MVC) webmethod and get a json response, so I can then iterate through a list with foreach (of d3.js)?

嘗試調用網絡方法並使用返回的json,以便可以在示例中從.csv文件交換以下內容:

http://mbostock.github.io/d3/talk/20111018/area-gradient.html

我可以使用jQuery返回json,但是回調“ data.foreach”中的代碼不起作用,因此未按照d3的格式進行格式化。 我該如何使用D3 (而非jQuery)將json庫從asp.net頁轉換為asmx Web服務,以便下面的data.foreach起作用? 當我嘗試序列化對象列表並以Json的身份返回時,使用D3只能返回null。 注意-返回一項是可以的(我可以創建並調用Web方法來返回GUID)。 但是為了使D3示例正常工作,我需要以示例所期望的格式返回json ...謝謝。 順便說一句,我可以使用MVC來做到這一點,盡管ASP.net還是需要它...

d3.csv("flights-departed.csv", function(data) {

// Parse dates and numbers.
data.forEach(function(d) {
d.date = parse(d.date);
d.value = +d.value;
});

// Compute the maximum price.
x.domain([new Date(1999, 0, 1), new Date(2003, 0, 0)]);
y.domain([0, d3.max(data, function(d) { return d.value; })]);

// Bind the data to our path elements.
svg.select("path.area").data([data]);
svg.select("path.line").data([data]);

draw();
});

更新

我整天都在嘗試解決這個問題。

根據以下文章,我更新了d3庫以​​允許調用web方法。

https://groups.google.com/d/msg/d3-js/-u_P9BF1COs/VP9Q1vaSJgAJ

現在,我可以調用並獲取JSON編碼的對象了,沒問題。 當Itry進行相同操作並返回對象列表時,它總是從webmathod返回為null。

解釋...

這有效:

function loadAnOBJECTViaJSonVIA_ASP_FRAMEWORK() {

    var sUrl = "http://localhost:3791/DemoServices.asmx/GetSingleObject_VIA_ASP_FRAMEWORK";

    alert('d3.json - ' + sUrl);

    d3.jsonasmx(sUrl, function (data) {

        var d = data.d;

        alert('I got the following OBJECT back:' + d);
        alert('reading property: ' + d.ppGuid);
        alert('reading property: ' + d.x_value);
        alert('reading property: ' + d.y_value);
    });
}

致電:

[WebMethod]
        public PointPair GetSingleObject_VIA_ASP_FRAMEWORK()
        {
            PointPair pp = new PointPair();

            return pp;
        }

這不起作用:

function loadOBJECT_Array_ViaJSonVIA_ASP_FRAMEWORK() {

            var sUrl = "http://localhost:3791/DemoServices.asmx/GetObjectArray_VIA_ASP_FRAMEWORK";

            alert('d3.json - ' + sUrl);

            d3.jsonasmx(sUrl, function (data) {

                var d = data.d;

                //alert('I got the following TOP LEVEL OBJECT back:' + d);

                //alert('reading property: ' + d.ppGuid);
                //alert('reading property: ' + d.x_value);
                //alert('reading property: ' + d.y_value);
            });
        }

致電:

[WebMethod]
        public PointPairList GetObjectArray_VIA_ASP_FRAMEWORK()
        {
            string testResultsID = "26881";
            List<PointPair> pointpairs = this.GetTestResultPointsPaired(testResultsID);

            PointPairList ppl = new PointPairList();

            ppl.ListOfPoints = pointpairs;

            return ppl;
        }

確實卡住了……在第二個示例中,回調函數數據為null。 我已經嘗試過將ut的點包裝在.ListOfPoints屬性中,就像你能看到的那樣,但這沒有幫助。

如果您獲取的數據為JSON格式,請使用d3.json()而不是d3.csv()來解析數據。

然后, 如果您的JSON格式化為對象數組,則其余代碼應能正常工作。 如果將其格式化為帶有命名條目的一個大對象,則還需要查看d3.entries()才能轉換為數組。

我不是100%肯定,但盡量確保你設置ResponseFormatResponseFormat.Json在您的Web方法。 JSON是一種標准,因此jQuery中可以使用的任何其他庫中都可以使用。 可能是JSON無效。

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
    public PointPairList GetObjectArray_VIA_ASP_FRAMEWORK()
    {
        string testResultsID = "26881";
        List<PointPair> pointpairs = this.GetTestResultPointsPaired(testResultsID);

        PointPairList ppl = new PointPairList();

        ppl.ListOfPoints = pointpairs;

        return ppl;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM