PDA

View Full Version : extJS and simple webservice in asp.net



mironline
23 Jul 2010, 4:08 AM
dear friends

after a couple of hours working , I can't get the picture of JsonStore's structure.
here is my code :


/// web service result class

public class WebServiceResult
{
public string Error { set; get; }
public object Result { set; get; }
}/// class FOO

public class Foo
{
public string foo { set; get; }
}/// webservice

public class Service : WebService {
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = true, XmlSerializeString = false)]
public WebServiceResult LoadData(object obj)
{
WebServiceResult result = new WebServiceResult();
List<Foo> post = new List<Foo> { new Foo(), new Foo() };
result.Result = post ;
return result;
}
}/// javascript


var _data = new Ext.data.JsonStore({
proxy: new Ext.data.HttpProxy({
url: '*.asmx/LoadData',
method: 'GET',
headers: { 'Content-type': 'application/json' },
baseParams: { obj: '' }
}),
root: 'd',
id: 'Id',
fields: ['Error', 'Result']
});
_data.load({ params: { obj: '1111'} });now my questions :


is my code has got any wrong syntax ?!
how can i Implement the fileds property
how can I loop through the result json ?


thank you very much
-mir

--miCZar--
7 Nov 2010, 3:11 PM
var _data = new Ext.data.JsonStore({
proxy: new Ext.data.HttpProxy({
url: 'Service.asmx/LoadData',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
baseParams: Ext.encode({ obj: '' })
}),
root: 'd',
//id: 'Id',
fields: ['Error', 'Result']
});
_data.load({ params: Ext.encode({ obj: '1111'}) });This should work. _data.data.items - this way u can loop through ws result
This may help - http://www.sencha.com/forum/showthread.php?114741-ExtJS-3.3-Calling-webservice-with-parameters-in-ASP.NET&highlight=ExtJS+3.3

Edit:

I didn't notice your WS is returning simple object. Ext.data.JsonStore is used for collections. If u wanna get simple object you can do it this way.
var wsParams = { obj: { IntProp: -1, StringProp: '0'} };
Ext.Ajax.request({
url: '/ws/Services.asmx/GetObject',
params: Ext.encode(wsParams),
headers: { 'Content-Type': 'application/json' },
method: 'POST',
success: function(result, request) {
Ext.MessageBox.alert('Success', 'Data from the server: ' + result.responseText);
var wsResult = Ext.decode(result.responseText);
},
failure: function(result, request) {
Ext.MessageBox.alert('Failed', 'Error while connecting to WS');
}
});Note if you are using ASP.NET > 2.0 then [I]var wsRetObj = wsResult.d;