jQuery.ajax 调用 服务(.aspx,.asmx)
方法1:Post(json)+IIS下;//aspx,适用本机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $.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,可解决跨域
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $.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,传统方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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,适用本机
1 2 3 4 5 6 7 8 9 10 11 12 13 | $.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不需要
附:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | /* 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); } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
2018-03-14 SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)
2018-03-14 sql server中QUOTENAME()函数的用法
2016-03-14 经典SQL语句大全