您的位置:首页 > 其它

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。。。。。等
如有错误请指正
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: