テキストファイルからデータを読み取る 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 点についてサポートをお願いします。
- この例で得たのと同じ結果を得るために使用する必要がある正しい JSON 形式は何ですか?
- 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);
}