sencha touch结合webservice读取jsonp数据详解

sencha touch读取jsonp数据主要依靠Ext.data.JsonP组件,在mvc的store文件中定义代码如下:

复制代码
Ext.define('eparkapp.store.ParksNearby',{
    extend:'Ext.data.Store',
    requires: ['Ext.data.JsonP'],
    config:{
        model: 'eparkapp.model.Park',
        autoLoad: true,
        proxy: {
            type: 'jsonp',
            url: 'http://192.168.1.103/androidserv/PublicAppServ.asmx/ListNearInfo',
            reader:{
                type: 'json',
                rootProperty: 'data'
            },
            extraParams: {
                userId: 'll',
                lng: '123',
                lat: '39',
                key: 'abc123'
            },
            callbackKey: 'callback'
        },
        sorters: [{ property: 'Range', direction: 'ASC'}]
    }
});
复制代码

其中重要的是url的写法,.asmx后增加webservice的方法名,传给webservice的参数使用extraParams配置项。另外callbackKey一定要加上,值可设置为默认的callback。

webservice的写法

复制代码
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Web.Script.Services;

namespace MobileServ
{
    /// <summary>
    /// PublicAppServ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    [System.Web.Script.Services.ScriptService]
    public class PublicAppServ : System.Web.Services.WebService
    {

        #region 服务
        
        [WebMethod(Description = "获得测试数据的列表")]
        public void ListNearInfo(string userId, string lng, string lat, string key)
        {
            HttpContext.Current.Response.ContentType = "application/json;charset=utf-8";
            string jsonCallBackFunName = "callback";
            if (HttpContext.Current.Request.Params["callback"]!=null)
                jsonCallBackFunName = HttpContext.Current.Request.Params["callback"].ToString();
            string result = string.Empty;
            if (key == ConfigurationManager.AppSettings["WSKey"])
            {
                List<ParkInfo> lisPark = GetData();
                result = "{success: true,data:[";
                for (int i = 0; i < lisPark.Count; i++)
                {
                    result += "{";
                    result += "Name:'" + lisPark[i].Name + "'";
                    result += ",Address:'" + lisPark[i].Address + "'";
                    result += ",Range:" + lisPark[i].Range;
                    result += ",Price:" + lisPark[i].Price;
                    result += ",Count:" + lisPark[i].Count.ToString();
                    result += "}";
                    if (i < lisPark.Count - 1)
                        result += ",";
                }
                result += "]}";
            }
            HttpContext.Current.Response.Write(string.Format("{0}({1})", jsonCallBackFunName, new JavaScriptSerializer().Serialize(result)));
            
        }

        private List<ParkInfo> GetData()
        {
            List<ParkInfo> lisPark = new List<ParkInfo>();
            ParkInfo pinfo1 = new ParkInfo();
            pinfo1.Name = "测试路1";
            pinfo1.Address = "测试地址1";
            pinfo1.Range = "2.5";
            pinfo1.Price = "5";
            pinfo1.Count = "83";
            lisPark.Add(pinfo1);
            ParkInfo pinfo2 = new ParkInfo();
            pinfo2.Name = "测试路2";
            pinfo2.Address = "测试地址2";
            pinfo2.Range = "3.2";
            pinfo2.Price = "10";
            pinfo2.Count = "12";
            lisPark.Add(pinfo2);
            ParkInfo pinfo3 = new ParkInfo();
            pinfo3.Name = "测试路3";
            pinfo3.Address = "测试地址3";
            pinfo3.Range = "3.2";
            pinfo3.Price = "10";
            pinfo3.Count = "12";
            lisPark.Add(pinfo3);
            return lisPark;
        }
        
        #endregion 服务
    }
}
复制代码

需要注意的是,webservice通过HttpContext.Current.Request.Params["callback"]来读取sencha touch从客户端发来的跨域请求的回调函数名callback。再把这个函数名加入到返回的json之前。实现客户端的跨域读取。

webservice的web.config配置节system.web下一定要加上如下配置项,否则sencha touch无法读取webservice的数据。

<webServices>
      <protocols>
        <add name="HttpPost"/>
        <add name="HttpGet"/>
      </protocols>
    </webServices>

一切配置完成后,数据就能在view中展现出来。

posted on   刑天  阅读(718)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示