·Customer类

public class Customer

{

public int Unid { get; set; }

public string CustomerName { get; set; }

public string Memo { get; set; }

public string Other { get; set; }

}

·服务端处理(Json_1.ashx)

Customer customer = new Customer { Unid=1,CustomerName="宋江",Memo="天魁星",Other="黑三郎"};

string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);

context.Response.Write(strJson);

jQuery.get( url, [data], [callback], [type] )

能过http get请求数据。其中的callback是$.ajax的一种缩略应用,指在成功加载数据后调用。

(一)ashx文件

Get请求得到json数据

·Ashx文件不再提供

·js

function GetCustomer_Ashx() {

$.get(

"webdata/get_1.ashx",

{},

function(data) {

var tt = "";

$.each(data, function(k, v) {

tt += k + ":" + v + "<br/>";

})

$("#divmessage").html(tt);

},

"json"

);

}

其中,参数如果没有,那么也要保留空参数列表;数据格式设置为json

(二)ashx文件,集合

·ashx文件不再提供,请见我的博客的其它随笔

·js

function GetCustomerList() {

$.get(

"webdata/get_1.ashx",

{},

function(data) {

var tt = "";

$.each(data, function(k, v) {

$.each(v, function(kk, vv) {

tt += kk + ":" + vv + "<br/>";

});

});

$("#divmessage").html(tt);

},

"json"

);

}

其中,没有参数,参数提供部分可以为空,也可以提供空列表;数据格式可以省略,写json也行,其它的几个选项也行。

(三)请求文本文件

这次得到文本内容,不得到json对象。

function GetCustomer_txt() {

$.get(

"webdata/get_1.txt",

function(data) {

$("#divmessage").html(data);

},

"text"

);

}

这里的数据类型,可以省略。

(四)请求WebService

http get方式请求web服务,默认get关闭。要手动启动。

可以在config文件中添加get支持:

<webServices>

<protocols>

<add name="HttpGet"/>

</protocols>

</webServices>

[WebMethod]

public string GetCustomer()

{

Customer customer = new Customer

{ Unid = 1, CustomerName = "宋江", Memo = "天魁星", Other = "黑三郎" };

string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);

return strJson;

}

ScriptMethod 属性标签用于指定用于调用方法的 HTTP 谓词以及响应的格式。此属性用于指定可从客户端脚本调用的方法的信息。使用此属性可以指定可用于调用方法的 HTTP 谓词(GET 或 POST)。它还使您可以指定是要使用 JavaScript 对象符号 (JSON) 还是 XML 对响应进行格式设置。

·UseHttpGet

指定是否要通过使用 HTTP GET 命令调用方法。默认值为 false。

·ResponseFormat

指定将响应序列化为 JSON 还是 XML。默认值为 Json。当方法返回 XmlDocument 或 XmlElement 对象时,ResponseFormat 属性可用于将 XML 指定为返回类型。

·XmlSerializeString

指定是否将所有返回类型(包括字符串类型)都序列化为 XML。将响应序列化为 JSON 时,将忽略 XmlSerializeString 属性的值。

如果web服务方法不修饰这个标签,那默认就序列化为json对象。

function GetCustomer_Webservice() {

$.get(

"get_1.asmx/GetCustomer",

function(data) {

var jsonObject = $.jsonToObject(data.text);

var tt = '';

$.each(jsonObject, function(k, v) {

tt += k + ":" + v + "<br/>";

});

$("#divmessage").html(tt);

},

"json"

);}

这种例子就举到这个为止。在ajax get请求web服务时,注意开通get协议访问。

最新文章

  1. 在 Visual Studio 等编辑器/IDE中自动切换输入法,不需要手动的有没有?
  2. codeforces 484E
  3. HTML5应用之文件拖拽上传
  4. 控制ASP.NET Web API 调用频率与限流
  5. MongoDB replicaSet
  6. 【风马一族_xml】Schema--- xml的约束技术
  7. JQuery笔记:JQuery和JavaScript的联系与区别
  8. Visual C++内存泄露检测—VLD工具使用说明
  9. 解决 winedit 打开tex文件 reading error
  10. 个人收集资料整理-WebForm
  11. Linux - 命令行 管道(Pipelines) 详细解释
  12. 双数组trie树的基本构造及简单优化
  13. Exiting the Matrix: Introducing Metasploit&#39;s Hardware Bridge
  14. TCP/IP笔记(六)TCP与UDP
  15. leetcode 717. 1-bit and 2-bit Characters -easy
  16. selenium中浏览器及对应的驱动(可下载)
  17. FASTMM内存泄漏处理
  18. Android UIAutomator 定位
  19. 微软职位内部推荐-Senior Dev Lead - SharePoint
  20. 一个钓鱼WiFi的破解

热门文章

  1. JVM类加载(1)—加载
  2. 报错apachectl restart
  3. mysql- 修改root密码的多种方法
  4. VIewPage+Fragment
  5. mysql中有多少种日志
  6. linux中历史命令的一点发现
  7. 2、Tophat align_summary.txt and samtools flagstat accepted_hits.bam disagree
  8. Centos6.8下yum安装python2.7
  9. 《精通Spring4.X企业应用开发实战》读后感第六章(使用外部属性文件)
  10. 8、scala函数式编程