Ajax跨域请求webservice
2015-07-27 17:18
211 查看
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请求
参数(具体)
var urlPar;
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。。。。。等
如有错误请指正
实例:请求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请求
参数(具体)
var urlPar;
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。。。。。等
如有错误请指正
相关文章推荐
- UVA12563 Jin Ge Jin Qu hao(DP, 背包+技巧)
- Android 软件开发之如何使用Eclipse Debug调试程序详解
- poj3463Sightseeing(最短路条数+次短路条数)
- Network
- phpMyadmin 修改上传文件大小限制
- Android—开发自学历程(3)-多线程
- 学与思
- monkey
- Network 分类: POJ 图论 2015-07-27 17:18 17人阅读 评论(0) 收藏
- nginx 反向代理 与 Apache backend的配置联合配置
- 在Windows中配置PHP的OpenSSL模块
- 【ASP.NET】Web中的Cookie写入与读取
- java使用正则表达式判断邮箱格式是否正确的方法
- android webView js方法和java 方法交互
- Space Shoot教程学习笔记
- hiho一下 Trie树
- HDMI之EDID字段解析
- hdoj1002A + B Problem II(大数加法)~
- Cocos2d-x精灵学习笔记
- 拟牛顿法 分析与推导