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 ...
随机推荐
- KVC/KVO原理详解及编程指南
一.简介 1.KVC简介 2.KVO简介 二.KVC相关技术 1.Key和Key Path 2.点语法和KVC 3.一对多关系(To-Many)中的集合访问器方法 4.键值验证(Key-Value V ...
- I.MX6 eMMC 添加分区
/********************************************************************************* * I.MX6 eMMC 添加分区 ...
- linux dns 连外网
以下设置对所用的Linux系统如Redhat/Ubuntu/Debian/CentOS等都有效,但您必须是管理员root或者具有管理员权限 vim /etc/resolv.conf 在其中加入: na ...
- C#编程语言与面向对象——核心
面向对象的核心 (1).封装 封装的类=数据+对此数据所进行的操作(即算法) 封装起外界不必需要知道的东西,指向外界展现可供展示的东西. 小到一个简单的数据结构,大到一个完整的软件子系统.静态的如某软 ...
- 分支语句 if的嵌套 循环语句
0930 今天学习内容做以下总结: 语句的分类:顺序语句,分支语句(选择,条件),循环语句 分支语句 格式1:if(表达式(要么是true 要么是false)){} 格式2:if(){}slse{} ...
- ASP.NET中数据邦定效率问题的一点看法 - 转载(自由的天空)
在 做Asp.NET开发的时候经常用到DataList.Repeater等,用这些控件的时候经常用到数据邦定,很多程序员都是按照MS提供的方 法<%#DataBinder.Eval(Contai ...
- 【C】 06 - 标准库概述
任何程序都会有一些通用的功能需求,对这些需求的实现组成了库.它可以提高程序的复用性.健壮性和可移植性,这也是模块化设计的体现.C规范定义了一些通用接口库,这里只作概述性介绍,具体细节当然还是要查阅规范 ...
- powerdsigner Association Multiplicity
这一篇來告诉一个不容易分辨的关系图式:Association(结合)的各种類型,除了了解它的涵义 外,也让各位可以看图說故事,知道它背后所要表达的意义. Association结合 Associati ...
- HA(High available)-Keepalived高可用性集群(双机热备)单点实验-菜鸟入门级
HA(High available)-Keepalived高可用性集群 Keepalived 是一个基于VRRP虚拟路由冗余协议来实现的WEB 服务高可用方案,虚拟路由冗余协议 (Virtual ...
- javascript 刷新当前页面
编辑器加载中... 1. window.location.replace(window.location.href)