Ajax跨域请求webservice
来源:互联网 发布:lua编程软件 编辑:程序博客网 时间:2024/06/06 07:19
web开发中ajax异步跨域请求,ajax是不支持跨域的,可以将ajax跨域转换为后台HttpWebRequest 请求
实例:请求Service.asmx中的Students方法 接受两个参数 startmonth和endmonth
实现跨域请求:
首先创建一个Handler
修改方法
public void ProcessRequest(HttpContext context)
{
HttpRequest request = context.Request;
HttpResponse response = context.Response;
string requestUrl = request.RawUrl.ToString();
if(requestUrl.Split((new char[1] { '?' })).Length == 1){
//请求地址错误
}
int index = requestUrl.IndexOf("requrl=") + "requrl=".Length;
string realUrl = requestUrl.Substring(index);
realUrl = HttpUtility.UrlDecode(realUrl, System.Text.Encoding.UTF8);
//参数和值
Stream streamData = request.InputStream;
StreamReader reader = new StreamReader(streamData, ASCIIEncoding.UTF8);
string postData = reader.ReadToEnd();
byte[] content = Encoding.UTF8.GetBytes(postData);
try
{
//发送HTTP请求,获取服务信息
Uri reqUrl = new Uri(realUrl);
HttpWebRequest mReq = (HttpWebRequest)WebRequest.Create(reqUrl);
//设置请求过期时间为10分钟
mReq.Timeout = 600000;
if (request.RequestType.ToLowerInvariant().Equals("post"))
{
mReq.Method = "POST";
//设置服务端需要返回的编码类型
mReq.ContentType = string.IsNullOrWhiteSpace(request.ContentType) ? "application/x-www-form-urlencoded" : request.ContentType;
if (content != null && content.Length > 0)
{
//发送请求
Stream requestStream = mReq.GetRequestStream();requestStream.Write(content, 0, content.Length);
requestStream.Close();
}
}
else if (request.RequestType.ToLowerInvariant().Equals("get"))
{
mReq.Method = "GET";
}
//返回请求结果
WebResponse mRes = mReq.GetResponse();
Stream mStream = mRes.GetResponseStream();
StreamReader sr = new StreamReader(mStream);
string strResult = sr.ReadToEnd();
context.Response.Write(strResult );
}
}
catch (System.Exception ee)
{
}
}
webservice中web.config中没有开启get和post请求
$.ajax({
url: "刚刚创建的handler名称.ashx?requrl=http://ip地址/Service.asmx/Students",//handler目录要写对 将请求的asmx传入handler
type: 'post',
data: “{参数1:‘参数1值’,参数2:‘参数2值’。。。。。}”,
dataType: "xml",//根据请求的webservice方法返回值类型定义
success: function (dt) {
}
}
这种请求方式是无法成功的(如果开启了,当然会请求成功)
配置文件中如果只开启了HttpSoap1.1和HttpSoap1.2协议:
<webServices>
<protocols>
<add name="HttpSoap1.2"/>
<add name="HttpSoap1.1"/>
</protocols>
</webServices>
HttpSoap1.1请求
参数(具体)
urlPar = '<?xml version="1.0" encoding="utf-8"?>';
urlPar = urlPar + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
urlPar = urlPar + '<soap:Body>';
urlPar = urlPar + '<Students xmlns="http://tempuri.org/">';
urlPar = urlPar + '<startmonth>';
urlPar = urlPar + 值;
urlPar = urlPar + '</startmonth>';
urlPar = urlPar + '<endmonth>';
urlPar = urlPar + 值;
urlPar = urlPar + '</endmonth>';
urlPar = urlPar + '</Students>';
urlPar = urlPar + '</soap:Body>';
urlPar = urlPar + '</soap:Envelope>';
HttpSoap1.2请求
参数(具体) var urlPar;
urlPar = '<?xml version="1.0" encoding="utf-8"?>';
urlPar = urlPar + '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/soap/envelope/">';
urlPar = urlPar + '<soap12:Body>';
urlPar = urlPar + '<Students xmlns="http://tempuri.org/">';
urlPar = urlPar + '<startmonth>';
urlPar = urlPar + 值;
urlPar = urlPar + '</startmonth>';
urlPar = urlPar + '<endmonth>';
urlPar = urlPar + 值;
urlPar = urlPar + '</endmonth>';
urlPar = urlPar + '</Students>';
urlPar = urlPar + '</soap12:Body>';
urlPar = urlPar + '</soap12:Envelope>';
发送ajax请求
$.ajax({
url: "刚刚创建的handler名称.ashx?requrl=http://ip地址/Service.asmx",//handler目录要写对 将请求的asmx传入handler
type: 'post',
contentType: "text/xml;charset=utf-8", //发送信息至服务器时内容编码类型 发送xml类型的内容时必须设置
data: urlPar,
dataType: "xml",
success: function (dt) {
//获取返回的数据
}
}
此方法不仅可以请求asmx还可以请求php、ashx、aspx。。。。。等如有错误请指正
- Ajax跨域请求webservice
- ajax跨域请求调用webservice接口
- WebService学习笔记-Ajax通过servlet请求跨域Webservice
- jquery ajax跨域请求webservice webconfig配置
- jquery ajax跨域请求webservice webconfig配置
- jquery ajax跨域请求webservice webconfig配置
- ajax请求webservice
- Ajax,Jquery请求WebService
- Web Service (014---webservice使用Jquery、Ajax请求WebService所遇到的跨域问题)
- 解决webservice用Ajax(jquery)传递客户端请求时ip跨域问题(异步域请求)
- jsonp跨域请求webservice+webservice部署
- ajax请求跨域请求
- WebService学习笔记-Ajax请求Webservice
- 利用JQuery jsonp实现Ajax跨域请求 .Net 的*.handler 和 WebService,返回json数据
- JQuery Ajax跨域请求 .Net 的*.handler 和 WebService,返回json数据
- 利用JQuery jsonp实现Ajax跨域请求 .Net 的*.handler 和 WebService,返回json数据
- 利用JQuery jsonp实现Ajax跨域请求 .Net 的*.handler 和 WebService,返回json数据
- ajax 跨域请求
- 二分图----最大匹配,最小点覆盖,最大点独立集
- 身为一个程序媛的深刻自我检讨
- phpMyadmin 修改上传文件大小限制
- 在线HTTP POST/GET接口测试工具 - aTool在线工具
- Window平台OpenGL渲染环境之使用GLUT搭建
- Ajax跨域请求webservice
- Android 软件开发之如何使用Eclipse Debug调试程序详解
- POJ 3414 Pots(BFS+回溯)
- hdoj2091空心三角形
- Django实例--英文、数字与中文之间增加空格
- opencv各类滤波器详解
- 【Unity3D游戏开发】之定制Debug.log输出 (八)
- ngx_cycle_s
- 变量声明和定义的关系