Why the web service can not directly return a JSON string?

  1. #1

    Why the web service can not directly return a JSON string?

    my code:
        <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:
    Click image for larger version. 

Name:	捕获.JPG 
Views:	202 
Size:	31.3 KB 
ID:	4191

    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}"}
  2. #2
    Take a look at this article
    http://encosia.com/asp-net-web-servi...serialization/

    I have found the webservices is very difficult to use when calling the code from a different domain, you can use a httphandler instead
    Also please note that WCF services will handle both cross domain and json out of the box

    hth /Peter
  3. #3
    Quote Originally Posted by plykkegaard View Post
    Take a look at this article
    http://encosia.com/asp-net-web-servi...serialization/

    I have found the webservices is very difficult to use when calling the code from a different domain, you can use a httphandler instead
    Also please note that WCF services will handle both cross domain and json out of the box

    hth /Peter
    Thanks for Peter.
    I have to switch httphandler to solve this problem.
    Perhaps maybe use a custom of the webservice, but I have not tested.
    Refer to the article:http://www.cnblogs.com/xiaoyin_net/a...8/1406071.html
    public class EnhancedWebService : System.Web.Services.WebService
        {
            public EnhancedWebService()
                : base()
            {
                string ServiceMethodName = GetMethodName();
                bool IsJSON = Context.Request.QueryString["form"] == "json";
                if (IsJSON) InterceptJSONMethodRequest(ServiceMethodName);
            }
            private string GetMethodName()
            {
                return Context.Request.Url.Segments[Context.Request.Url.Segments.Length - 1];
            } 
            private void InterceptJSONMethodRequest(string ServiceMethodName)
            { 
                JSONMethodAttribute JMA = GetMethodJSONMethodAttribute(ServiceMethodName);
                if (JMA == null)
                {
                    Context.Response.Write("throw new Exception('The Web Service method " + 
                                           ServiceMethodName + " is not available " + 
                                           "as a JSON function. For more " + 
                                           "information contact " + 
                                           "the Web Service Administrators.');");
                }
                else
                { 
                    //ToDo: deserialize parameters, call target method, 
                    //      deserialize and write return value
                }
                Context.Response.Flush();
                Context.Response.End();
            } 
            private JSONMethodAttribute GetMethodJSONMethodAttribute(string WebServiceMethodName)
            {
                MethodInfo ActiveMethod = this.GetType().GetMethod(WebServiceMethodName);
                JSONMethodAttribute JMA = null;
                if (ActiveMethod != null)
                {
                    object[] Attributes = ActiveMethod.GetCustomAttributes(true);
                    foreach (object Attribute in Attributes)
                    {
                        if (Attribute.GetType() == typeof(JSONMethodAttribute))
                        {
                            JMA = (JSONMethodAttribute)Attribute;
                        }
                    }
                }
                return JMA;
            }
        }
    }

Similar Threads

  1. [CLOSED] Store: Passing JSON data directly to Server-Side
    By nhg_itd in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Feb 09, 2012, 2:08 AM
  2. Problem CRUD with JSON&Web Service
    By oseqat in forum 1.x Help
    Replies: 1
    Last Post: Jul 12, 2011, 10:23 AM
  3. Replies: 4
    Last Post: Feb 01, 2011, 11:54 AM
  4. [CLOSED] How to deal with a null date in a JSON Web Service?
    By dev in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Aug 31, 2010, 11:51 PM
  5. Replies: 3
    Last Post: Dec 11, 2009, 11:48 AM

Tags for this Thread

Posting Permissions