asp.net项目下的web service返回json数据问题
App_Code目录下放置WebService.cs文件,文件内容如:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.ServiceModel.Web;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI.WebControls; [WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public void GetStores(string deliveryStartTime, string deliveryEndTime, bool is20To22 = false, string cityPOSCodePrefix = "")
{
var storeListJson = new StringBuilder(); //
var storeList = GetStoreList(deliveryStartTime, deliveryEndTime, is20To22, cityPOSCodePrefix);
storeListJson.Append("[");
foreach (var store in storeList)
{
string format = "{{\"Value\":\"{0}\", \"Text\":\"{1}\"}}"; storeListJson.Append(string.Format(format, store.Value.ToString(), store.Text.ToString()));
storeListJson.Append(",");
}
storeListJson.Remove(storeListJson.Length - , );
storeListJson.Append("]");
// Context.Response.Clear();
Context.Response.ContentType = "text/json; charset=utf-8";
//Context.Response.AddHeader("content-disposition", "attachment; filename=export.json");
//Context.Response.AddHeader("content-length", storeListJson.Length.ToString());
Context.Response.Flush();
Context.Response.Write(storeListJson);
}
}
WebService.asmx文件可以放在网站跟目录下,跟目录下的页面请求WebService拿json数据时:
var url = "/webservice.asmx/GetStores?cityPOSCodePrefix=" + cityPOSCodePrefix + "&deliveryStartTime=" + deliveryStartTime
+ "&deliveryEndTime=" + deliveryEndTime + "&is20To22=" + is20To22;
js对请求返回的数据进行处理,如下:
$.ajax({
url: url,
type: 'get',
async: false,
contentType: 'text/json; charset=utf-8',
success: function (result) {
//alert(result.d[0]);
var storeListString = "";
var storeLiValueStart = "<li style=\"float: left; margin-left: 40px; margin-top: 8px; width: 120px; border-radius: 8px; background-color: #FFF; border: 2px solid rgb(0,119,175);color:#0077af; padding: 4px 0px; text-align: center; cursor: pointer\" id=\"";
var storeLiValueEndAndTextStart = "\">";
var storeLiTextEnd = "</li>"; for (var i = ; i < result.length; i++) {
storeListString += storeLiValueStart;
storeListString += result[i].Value;
storeListString += storeLiValueEndAndTextStart;
storeListString += result[i].Text;
storeListString += storeLiTextEnd;
} $("#storeListTest").html(storeListString);
}
注意:要想拿到返回的json数据,web.config文件中system.web节点下必须加如下节点:
<webServices>
<protocols>
<add name="HttpGet" />
</protocols>
</webServices>
asp.net项目下的web service返回json数据问题的更多相关文章
- Web API返回JSON数据
对Web API新手来说,不要忽略了ApiController 在web API中,方法的返回值如果是实体的话实际上是自动返回JSON数据的例如: 他的返回值就是这样的: { "Conten ...
- .Net Web Api返回Json数据中原对象变量名大小写问题
这两天在工作中使用SignalR的WebSocket做数据实时传递的功能开发,在后端主动向前端广播数据以Json传递时,前端获取的Json中对应类的变量名首字母默认传递的是大写.而前端一直获取到的后台 ...
- [转贴] ASP.NET -- Web Service (.asmx) & JSON
[转贴] ASP.NET -- Web Service (.asmx) & JSON 以前没做过,但临时被要求 ASP.NET Web Service 要传回 JSON格式 找到网络上两篇好文 ...
- .net mvc web api 返回 json 内容,过滤值为null的属性
原文:http://blog.csdn.net/xxj_jing/article/details/49508557 版权声明:本文为博主原创文章,未经博主允许不得转载. .net mvc web ap ...
- J2EE Web开发入门—通过action是以传统方式返回JSON数据
关键字:maven.m2eclipse.JSON.Struts2.Log4j2.tomcat.jdk7.Config Browser Plugin Created by Bob 20131031 l ...
- MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式。
原文链接:https://www.muhanxue.com/essays/2015/01/8623699.html MVC web api 返回JSON的几种方式 1.在WebApiConfig的Re ...
- iOS开发网络篇之Web Service和XML数据解析
郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主.捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...
- ASP.NET Core中返回 json 数据首字母大小写问题
ASP.NET Core中返回 json 数据首字母大小写问题 在asp.net core中使用ajax请求动态绑定数据时遇到该问题 后台返回数据字段首字母为定义的大写,返回的数据没有问题 但是在前台 ...
- idea+springmvc+spring+mybatis+maven整合返回json数据webapi
首先看一张目录结构图: : 创建步骤: 1.创建maven webapp工程, 创建完后的目录结构为: 2.添加项目依赖(添加jar包) 需要的jar包: spring-webmvc, spring ...
随机推荐
- Android 学习第2课,下载 eclipse 工具
可以到http://www.ddooo.com/softdown/61745.htm 下载下来是32位与64位都有的 而且是汉化的,经测试成功,还可以,不错!
- Kubuntu 使用YaH3C进行中大校园网认证
之前都是用路由器连网线上网,我也没注意到inode校园网客户端在linux上的问题.直到前两天把路由器给搞残废了,只能默默的找办法装inode.根据学校网络中心给的教程,在kubuntu上尝试安装in ...
- 动态添加Marquee标签,并动态赋值与属性
前台加载js $(function(){ var publishStr="<%=publishText%>" var marqueeStr=" <mar ...
- MVC的用法和作用
最近在学习IOS项目的时候,老师经常提起MVC,在理解的过程中,越来越发现MVC的魅力,MVC:M:Model V:View C:Controller:Model 是用来存储数据的,View 是用来显 ...
- 《JavaScript面向对象编程指南》译者序
相对于Perl.Python等动态脚本语言来说,JavaScript确实是一门饱受误解的语言.对于译者这种从20世纪90年代末走过来的C++程序员来说,尤其如此.在那个年代,提起JavaScript总 ...
- vb6 枚举对象属性
Option Explicit '引用Library TLI ' C:\WINDOWS\system32\TLBINF32.DLL ' TypeLib Information Private Sub ...
- unity, terrain道出为obj
http://wiki.unity3d.com/index.php?title=TerrainObjExporter
- jquery datatables双击,获取行号。
function dbClickDatatables(rows) { $("#@(Perfix)tbData tbody tr").dblclick(function(e){ de ...
- 如何成为一名合格甚至优秀的个人草根站长(转载自ChinaZ)
这章本来不想写来的,后来琢磨琢磨还是废话一下吧.主要是想说下现在草根站长的状态和如何成为一名合格的甚至优秀的草站站长. 伟大的草根站长们,在某些媒体的超级忽悠下全来到网络上淘金来了,有在校的大学生,有 ...
- Delphi ActiveX Form的使用实例
Delphi ActiveX Form的使用实例 By knityster 1. ActiveX控件简介 ActiveX控件也就是一般所说的OCX控件,它是ActiveX技术的一部分. ActiveX ...