0

テキストファイルからデータを読み取る Ext.data.TreeStore があります。

ここに画像の説明を入力

this.store = Ext.create('Ext.data.TreeStore', {
        model: "GeoExt.data.LayerTreeModel",
        proxy: new Ext.data.HttpProxy({
            url: 'data/test.json',
            reader: {
                type: 'json',
                root: 'children',
                idProperty: 'Id'
            }
        }),
        folderSort: true
    });

これはファイルの内容です:

[
    {
        text: "Category1",
        leaf: false,
        expanded: true,
        checked: false,
        children: [
            {
                text: "A1",
                layer: "A2",
                name: "A3",
                leaf: true,
                checked: false,
                nodeType: "gx_layer"
            },
            {
                text: "B1",
                layer: "B2",
                name: "B3",
                leaf: true,
                checked: false,
                nodeType: "gx_layer"
            }
            ],
        nodeType: "gx_layer"
    },
    {
        text: "Category2",
        leaf: false,
        expanded: true,
        checked: false,
        children: [
            {
                text: "C1",
                layer: "C2",
                name: "C3",
                leaf: true,
                checked: false,
                nodeType: "gx_layer"
            }],
        nodeType: "gx_layer"
    }
]

get が JSON struct を返すのではなく、プレーンテキストを返すことに firebug で気付きました。しかし、それは機能します。

ここに画像の説明を入力

同じ結果を取得したいのですが、webservice からツリー ノードを取得します (postgres から戻します)。

私が試した最初の最も簡単なことは(テキスト/ jsonも試しました):

[WebMethod]        
        public void GetJSON()
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "text/plain";
            HttpContext.Current.Response.Charset = "utf-8";

            string res = File.ReadAllText(@"D:\test.json");
            HttpContext.Current.Response.Write(res);
        }     

そして、私はエラーが発生しています:

Request format is unrecognized for URL unexpectedly ending in '/getJSON'.

ファイルが JSON ではないため、これは理にかなっています (しかし、これが私がなんとか機能させる方法でした)。

次の 2 点についてサポートをお願いします。

  1. この例で得たのと同じ結果を得るために使用する必要がある正しい JSON 形式は何ですか?
  2. webservice から同じデータを提供するにはどうすればよいですか?

ありがとうございました !

***************** 解決 ******************

Meister answer に基づいて、メソッドは次のようになる必要があります。

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]       
public void GetJSON()
{
Object res = new Object();
JavaScriptSerializer js = new JavaScriptSerializer();
string str = js.Serialize(res);
Context.Response.Clear();
Context.Response.ContentType = "application/json";
Context.Response.AddHeader("content-disposition", "attachment; filename=export.json");
Context.Response.Flush();
Context.Response.Write(str);
} 
4

1 に答える 1

0

置換を試すことができます:

HttpContext.Current.Response.ContentType = "text/plain";

これとともに:

HttpContext.Current.Response.ContentType = "application/json";

更新:[ScriptService] Web サービス クラスの上に 追加することもできます。

とにかく、Web サービスから JSON ファイルを読み取るのは意味がないと思います。最初のコード サンプルで行ったように、JSON リーダーから読み取ることができます。Web サービスを使用して、渡されたパラメーターを処理し、何か (JSON オブジェクトなど) を作成して応答を返します。

于 2013-10-31T02:49:01.823 に答える