May 03, 2012, 11:26 AM
Why the web service can not directly return a JSON string?
my code:
json string format seems to have been damaged.
<ext:ComboBox ID="OrderNo"
runat="server"
DisplayField="sOrder_No"
ValueField="sOrder_No"
TypeAhead="false"
LoadingText="Searching..."
Width="150" ListWidth="400"
PageSize="10"
HideTrigger="true"
ItemSelector="tr.list-item"
MinChars="2" FieldLabel="采购单号">
<Store>
<ext:Store ID="Store1" runat="server" AutoLoad="false">
<Proxy>
<ext:HttpProxy Json="true" Method="POST" Url="../../Common/Query.asmx/PagingQuery" />
</Proxy>
<BaseParams>
<ext:Parameter Name="querytype" Value="OrderNo" Mode="Value" />
</BaseParams>
<Reader>
<ext:JsonReader Root="d.Data" TotalProperty="d.TotalRecords">
<Fields>
<ext:RecordField Name="sOrder_No" />
<ext:RecordField Name="nBuy_Date" />
<ext:RecordField Name="sFac_Id" />
<ext:RecordField Name="sFac_Name" />
</Fields>
</ext:JsonReader>
</Reader>
</ext:Store>
</Store>
<Template ID="Template1" runat="server">
<Html>
<tpl for=".">
<tpl if="[xindex] == 1">
<table class="cbStates-list">
<tr>
<th>采购单号</th>
<th>采购日期</th>
<th>厂商编码</th>
<th>厂商名称</th>
</tr>
</tpl>
<tr class="list-item">
<td style="padding:3px 3px;">{sOrder_No}</td>
<td>{nBuy_Date}</td>
<td>{sFac_Id}</td>
<td>{sFac_Name}</td>
</tr>
<tpl if="[xcount-xindex]==0">
</table>
</tpl>
</tpl>
</Html>
</Template>
</ext:ComboBox>
WebService:using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using DBUtility;
using System.Data;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using Ext.Net;
namespace Web
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class Query : System.Web.Services.WebService
{
[WebMethod]
public string PagingQuery(int start, int limit, string query, string querytype)
{
int count=0;
DataTable dt = GetOrderList(query, start, limit, out count);
string result = JsonConvert.SerializeObject(dt, new DataTableConverter());
return string.Format("{{Data:{0},TotalRecords:{1}}}", result, count);
}
private DataTable GetOrderList(string query, int start, int limit, out int count)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("sOrder_No", typeof(string)));
dt.Columns.Add(new DataColumn("nBuy_Date", typeof(string)));
dt.Columns.Add(new DataColumn("sFac_Id", typeof(string)));
dt.Columns.Add(new DataColumn("sFac_Name", typeof(string)));
dt.Rows.Add("9TC09000133", "20090505", "1F019", "Dong Guan");
dt.Rows.Add("9TC09000134", "20090504", "1F018", "Tong Tuan");
count = 100;
return dt;
}
}
}
error meeage:json string format seems to have been damaged.
{"d":"{Data:[{\"sOrder_No\":\"9TC09000133\",\"nBuy_Date\":\"20090505\",\"sFac_Id\":\"1F019\",\"sFac_Name\":\"Dong Guan\"},{\"sOrder_No\":\"9TC09000134\",\"nBuy_Date\":\"20090504\",\"sFac_Id\":\"1F018\",\"sFac_Name\":\"Tong Tuan\"}],TotalRecords:100}"}