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数据问题的更多相关文章

  1. Web API返回JSON数据

    对Web API新手来说,不要忽略了ApiController 在web API中,方法的返回值如果是实体的话实际上是自动返回JSON数据的例如: 他的返回值就是这样的: { "Conten ...

  2. .Net Web Api返回Json数据中原对象变量名大小写问题

    这两天在工作中使用SignalR的WebSocket做数据实时传递的功能开发,在后端主动向前端广播数据以Json传递时,前端获取的Json中对应类的变量名首字母默认传递的是大写.而前端一直获取到的后台 ...

  3. [转贴] ASP.NET -- Web Service (.asmx) & JSON

    [转贴] ASP.NET -- Web Service (.asmx) & JSON 以前没做过,但临时被要求 ASP.NET Web Service 要传回 JSON格式 找到网络上两篇好文 ...

  4. .net mvc web api 返回 json 内容,过滤值为null的属性

    原文:http://blog.csdn.net/xxj_jing/article/details/49508557 版权声明:本文为博主原创文章,未经博主允许不得转载. .net mvc web ap ...

  5. J2EE Web开发入门—通过action是以传统方式返回JSON数据

    关键字:maven.m2eclipse.JSON.Struts2.Log4j2.tomcat.jdk7.Config Browser Plugin Created by Bob 20131031 l ...

  6. MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式。

    原文链接:https://www.muhanxue.com/essays/2015/01/8623699.html MVC web api 返回JSON的几种方式 1.在WebApiConfig的Re ...

  7. iOS开发网络篇之Web Service和XML数据解析

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主.捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...

  8. ASP.NET Core中返回 json 数据首字母大小写问题

    ASP.NET Core中返回 json 数据首字母大小写问题 在asp.net core中使用ajax请求动态绑定数据时遇到该问题 后台返回数据字段首字母为定义的大写,返回的数据没有问题 但是在前台 ...

  9. idea+springmvc+spring+mybatis+maven整合返回json数据webapi

    首先看一张目录结构图: : 创建步骤: 1.创建maven  webapp工程, 创建完后的目录结构为: 2.添加项目依赖(添加jar包) 需要的jar包: spring-webmvc, spring ...

随机推荐

  1. php轮流排序,每隔一定的时间轮流进行位置排序,轮询的排行榜:function dataPollingInterval()

    /* * @名称: php ,对数组每隔一定的时间(自设定时间)来轮流进行位置排序,轮询的排行榜. 精确到指定的秒 或 分钟 或 小时 或 天 ,对数据列表进行轮排. * @参数: (array)$l ...

  2. I.MX6 eMMC 添加分区

    /********************************************************************************* * I.MX6 eMMC 添加分区 ...

  3. 基于cocos2d-x的Android游戏中使用fmod音频引擎

    cocos2d-x的音频引擎是cocosDenshion, 它的Android版比较弱, 只能播放一个背景音乐和些许音效, 如果要实现稍微复杂一点的音频播放, 比如同时播放几个音轨就不能了. 这一点远 ...

  4. eclipse生成doc文件乱码

    正确生成:在所写项目右击----Export----Java----javadoc---next ----一直next---finish. 乱码时:1)右击----Export----Java---- ...

  5. HTML5实战——svg学习

    百度百科: SVG可缩放矢量图形(Scalable Vector Graphics)是基于可扩展标记语言(XML),用于描述二维矢量图形的一种图形格式.SVG是W3C制定的一种新的二维矢量图形格式,也 ...

  6. POJ 1006 中国剩余定理

    #include <cstdio> int main() { // freopen("in.txt","r",stdin); ; while(sca ...

  7. cocoapod的下载安装解释

    本文不提供cocoapod的下载安装的流程,因为那些只要百度一下就有的东西,而是对里面的代码进行解释,希望对iOS小白安装cocoapod有帮助: 一.cocoapod是什么? 开发过程中,我们会用到 ...

  8. 【转】关于.net framework4.0以及4.5安装失败,“安装时发生严重错误”……

    也不知道管不管用,先记着 今天忽然想装一个vs2010,然后装了好几遍,每次都在安装.net4.0的时候失败.好吧,我自己手动装行么.于是手动去装.net 4.0. 结果在还是返回"安装时发 ...

  9. RequestContextListener作用

    spring IOC容器实例化Bean的方式有: singleton            在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype     ...

  10. 合并excel-MergeExcel

    MergeExcel 将需要合并的excel放入该目录下,包含xls和xlsx 点击运行按钮: 输入包含多少列: 等待运行,完毕! 基于python编写,源码和exe于 TTyb