方法1:Post(json)+IIS下;//aspx,适用本机

        $.ajax({
url: "TpgConnect_Rain/postgreService.aspx/getRainInfo",
type: "POST", //使用Post方式请求
contentType: "application/json;charset=utf-8",
data: "{}", //这里是要传递的参数,格式为 data: "{value1:'心想事成',i:10}"
dataType: "json",
success: function (result) {
datas_rain = JSON.parse(result.d);
alert(datas_rain);
},
error: function (x, e) {
alert("error:" + x.responseText);
}
});

方法2:Get(jsonp);//aspx,可解决跨域  

        $.ajax({
url: "http://10.19.1.54/TpgConnect_Rain/postgreService.aspx/getRainInfo?method=rain",
type: "GET", //使用GET方式请求
dataType: "JSONP",
jsonp: "callback",
data: "{}",
success: function (result) {
datas_rain = JSON.parse(result);
alert(datas_rain);
},
error: function (x, e) {
alert("error:" + x.responseText);
}
});

方法3:XMLHttpRequest;//aspx,传统方式

        var xmlhttp = new XMLHttpRequest();
window.onload = function () {
var url = "http://localhost/TpgConnect/postgreService.aspx?method=rain";
xmlhttp.open("post", url, true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = updatePage;
xmlhttp.send(null);
} function updatePage() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200 || xmlhttp.status == 0) {
alert(eval(xmlhttp.responseText)[0].stationno);
}
else
alert(xmlhttp.status);
}
}

方法4:Post(json)+IIS下;//asmx,适用本机 

        $.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: "WebService.asmx/GetWish",
data: "{value1:'心想事成',value2:'万事如意',value3:'牛牛牛',value4:2009}",
dataType: "json",
success: function (json) {
alert("success" + json.d);
},
error: function (x, e) {
alert("error" + x.responseText);
}
});
说明:
1)尽量使用post,无参时:data: "",将返回xml格式;data: "{}",将返回json格式
2)get参数有中文时需要编码,而post不需要
 
附:

        /* ASP.NET服务源码(aspx)*/
protected void Page_Load(object sender, EventArgs e)
{
Response.AddHeader("Access-Control-Allow-Origin", "*");
string result = "";
var val1 = Request.QueryString["val1"];
var val2 = Request.QueryString["val2"];
var val3 = Request.QueryString["val3"];
var callback = Request.QueryString["method"];
if (callback == "rain")
result = getRainInfo();
else if (callback == "pond")
result = getPondInfo();
else if (callback == "snow")
result = getSnowInfo();
Response.Write(result);
Response.End();
} /* WebService服务源码(asmx) */
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
}
[WebMethod]
public string GetWish(string value1, string value2, string value3, int value4)
{
return string.Format("祝您在{3}年里 {0}、{1}、{2}", value1, value2, value3, value4);
}
}

  

  

 

  

jQuery.ajax 调用 服务(.aspx,.asmx)的更多相关文章

  1. jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)

    在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. [WebMethod] public static string SayHe ...

  2. Jquery ajax调用后台aspx后台文件方法(不是ashx)

    在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. (1)通过aspx.cs的静态方法+WebMethod进行处理 简单的介绍下 ...

  3. Jquery Ajax调用aspx页面方法

    Jquery Ajax调用aspx页面方法 在asp.net webform开发中,用jQuery ajax传值一般有几种玩法 1)普通玩法:通过一般处理程序ashx进行处理: 2)高级玩法:通过as ...

  4. jquery.ajax请求aspx和ashx的异同 Jquery Ajax调用aspx页面方法

    1.jquery.ajax请求aspx 请求aspx的静态方法要注意一下问题: (1)aspx的后台方法必须静态,而且添加webmethod特性 (2)在ajax方法中contentType必须是“a ...

  5. WCF入门教程(四)通过Host代码方式来承载服务 一个WCF使用TCP协议进行通协的例子 jquery ajax调用WCF,采用System.ServiceModel.WebHttpBinding System.ServiceModel.WSHttpBinding协议 学习WCF笔记之二 无废话WCF入门教程一[什么是WCF]

    WCF入门教程(四)通过Host代码方式来承载服务 Posted on 2014-05-15 13:03 停留的风 阅读(7681) 评论(0) 编辑 收藏 WCF入门教程(四)通过Host代码方式来 ...

  6. Jquery Ajax调用asmx出错问题

    1.//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释.      [System.Web.Script.Services.ScriptService] 这个 ...

  7. JQuery ajax调用asp.net的webMethod

    本文章转载:http://www.cnblogs.com/zengxiangzhan/archive/2011/01/16/1936938.html 在vs2010中,用JQuery ajax调用as ...

  8. Jquery ajax调用webservice总结

    jquery ajax调用webservice(C#)要注意的几个事项: 1.web.config里需要配置2个地方 <httpHandlers>      <remove verb ...

  9. Jquery Ajax 调用 WebService

    原文:http://www.cnblogs.com/andiki/archive/2010/05/17/1737254.html jquery ajax调用webservice(C#)要注意的几个事项 ...

随机推荐

  1. STRING DELIMITED BY SIZE

    The following COBOL program depicts the use of  STRING DELIMITED BY SIZE                 IDENTIFICAT ...

  2. OI常用重载运算符

    用STL时经常用到 PS:这里有个巨坑,把重载写在STL里常数会爆炸,分分钟大数据TLE 所以就把重载放在结构体里啦 //以priority_queue的重载为例 //priority_queue默认 ...

  3. Python应用——自定义函数:分割PDF文件函数

    案例 将一个 pdf 文件按要求分割为几个部分.比如说一个pdf有20页,分成5个pdf文件,每个pdf文件包含4页.设计函数实现? Python代码 from PyPDF2 import PdfFi ...

  4. Scrapy基础(六)————Scrapy爬取伯乐在线一通过css和xpath解析文章字段

    上次我们介绍了scrapy的安装和加入debug的main文件,这次重要介绍创建的爬虫的基本爬取有用信息 通过命令(这篇博文)创建了jobbole这个爬虫,并且生成了jobbole.py这个文件,又写 ...

  5. mongoose+koa2 按照_id更新多条数据,删除数组中的字段,然后添加新的字段,$pull和$or结合使用

    await model.photo.update({ _id: { $in: photoIdsParam } }, { $pull: { customerIds: { code: custCode, ...

  6. 获取当前泛型类的传入,BaseDaoImpl<T> implements BaseDao<T>

    public abstract class BaseDaoImpl<T> implements BaseDao<T> { private Class<T> claz ...

  7. bzoj3255 一个关于序列的游戏

    题意是啥 给你一个数列,可以任意删去一段,记其长度为$s$,得到$val_s$的价值,问你最大价值和为多少.. 其中这一段数要满足成一个上凸且相邻数差为$1$ 显然,删掉一段数后剩下的左右会相邻.. ...

  8. sench touch 自定义小图标(转)

    自定义图标的方法 Sencha touch自带图标有限,有时需要自己添加图标.下面介绍自定义图标的方法: 首先需要生成图标字体.有许多网站提供在线生成图标字体的功能,比如IcoMoon,通过这个网站, ...

  9. HEVC与VP9之间的对比

    在streamingmedia上看到的一篇对比HEVC与VP9的文章,挺不错.另外这边文章的几个comment也是不错的. 下面是全文.   The Great UHD Codec Debate: G ...

  10. Cocos Creator学习笔记

    1.动态加载图片 cc.Class({ extends: cc.Component, properties: { label: { default: null, type: cc.Label }, l ...