ajax访问WebService跨域问题
1、先看一个网站介绍,了解跨域问题 HTTP访问控制(CORS)
2、像谷歌、火狐浏览器对一些非简单请求会触发预检请求,首先使用 OPTIONS 方法发起一个预检请求到服务器,然而IE浏览器没有预检请求

3、发起预检请求,如果想要后台处理成功,那么就需要服务器处理返回响应,设置允许的请求头,设置允许跨域等(对WebService研究较浅,没有找到对预检请求设置的方法,后期会深入学习)
4、在测试中使用谷歌,如果不设置contenttype,默认为 text/plain;charset=UTF-8或application/x-www-form-urlencoded,更改为其他两个都不会触发预检请求
5、谷歌浏览器ajax设置contenttype为text/xml时,对JDK发布的WebService和CXF发布的WebService错误显示如下
JDK发布的WebService服务
1)后台报错、找到com.sun.xml.internal.ws.transport.http.server.WSHttpHandler这个类,在handle方法处打断点,可以在参数中看到请求的信息,method为options;然后运行完控制台报错(使用IE就没事)
com.sun.xml.internal.ws.transport.http.server.WSHttpHandler handleExchange
警告: Cannot handle HTTP method: OPTIONS
这就是浏览器的预检请求导致,网上有设置如果检测到请求方法是OPTIONS就设置返回状态为200


2)前台报错、console下报错:OPTIONS http://localhost:8088/aaa net::ERR_EMPTY_RESPONSE
然后在network下找到发送的ajax请求,点击,Request Headers有警告Provisional headers are shown

正常情况下请求头会显示一些其他信息如Accept-Language、Accept-Encoding等
3)ajax不设置contenttype、请求后台就报错
Unsupported Content-Type: text/plain;charset=UTF-8 Supported ones are: [text/xml]
com.sun.xml.internal.ws.server.UnsupportedMediaException: Unsupported Content-Type: text/plain;charset=UTF-8 Supported ones are: [text/xml]
也就是建议设置Content-Type为text/xml;但是设置成text/xml就会触发预检测
前台Console报错 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8083' is therefore not allowed access. The response had HTTP status code 415.估计是后台异常,没有设置响应头非同源访问的权限
此处可以看到请求头默认Content-Type为text/plain;charset=UTF-8

CXF发布的WebService服务
1)、后台报错、找到类org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor断点到handleMessage方法,查看message参数
后台报错
Interceptor for {http://server.hjp.com/}PersonService1Service has thrown exception, unwinding now
org.apache.cxf.binding.soap.SoapFault: Error reading XMLStreamReader.

可见也是预检测请求导致
2)前端报错:OPTIONS http://localhost:5555/hello 500 (Server Error)和Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. network中的ajax请求头中没有Content-Type

3)如果去掉ajax对contenttype设置,前端会报错No 'Access-Control-Allow-Origin' header is present on the requested resource

6、ajax请求WebService代码(使用IE访问没问题)
1)、jQuery形式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="jquery-3.1.0.min.js"></script>
</head>
<body>
<div id="d"></div>
<script type="text/javascript">
$.ajax({
url: "http://localhost:5555/hello",
type: "POST",
contentType:"text/xml;charset=UTF-8",
data: getPostData(),
success: function (data) {
var doc = $(data).find('return');
doc.each(function(){
$('#d').html($(this).text());
});
}
});
//定义满足SOAP协议的参数。
function getPostData() {
var soap='<?xml version="1.0" encoding="UTF-8"?>'+
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://server.hjp.com/">' +
'<soapenv:Header/>' +
'<soapenv:Body>' +
'<ser:sayHello>' +
'<arg0>aaa</arg0>' +
'</ser:sayHello>' +
'</soapenv:Body>' +
'</soapenv:Envelope>';
return soap;
}
</script>
</body>
</html>
2)、原生Ajax形式
<html>
<head>
<meta charset="UTF-8"/>
<title>通过ajax调用WebService服务</title>
<script>
function getXhr(){
var xhr = null;
if(window.XMLHttpRequest){
//非ie浏览器
xhr = new XMLHttpRequest();
}else{
//ie浏览器
xhr = new ActiveXObject('Microsoft.XMLHttp');
}
return xhr;
}
var xhr =getXhr();
function sendMsg(){
var name = document.getElementById('name').value;
//服务的地址
var wsUrl = 'http://localhost:8088/tongwei'; //请求体
var soap=getPostData(name); //打开连接
xhr.open('POST',wsUrl,true); //重新设置请求头
xhr.setRequestHeader("content-type","text/xml"); //设置回调函数
xhr.onreadystatechange = _back; //发送请求
xhr.send(soap);
} function _back(){
if(xhr.readyState == 4){
if(xhr.status == 200){
var ret = xhr.responseXML;
var msg = ret.getElementsByTagName('return')[0];
document.getElementById('showInfo').innerHTML = msg.textContent;
}
}
}
//定义满足SOAP协议的参数。
function getPostData(name) {
var postdata ='<?xml version="1.0" encoding="UTF-8"?>'+
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ton="http://tongwei/">' +
'<soapenv:Header/>' +
'<soapenv:Body>' +
'<ton:test>' +
'<parStr>'+name+'</parStr>' +
'</ton:test>' +
'</soapenv:Body>' +
'</soapenv:Envelope>';
return postdata;
}
</script>
</head>
<body>
<input type="button" value="发送SOAP请求" onclick="sendMsg();">
<input type="text" id="name">
<div id="showInfo">
</div>
</body>
</html>
7、java后台URL请求WebService方式
public static void main(String[] args) throws IOException {
/*
*1.创建一个url
*2.打开一个连接
*3.设置相关参数
*4.创建输出流,用来发送SAOP请求
*5.发送完,接收数据
*6.用输入流获取webservice中的内容
*/
URL url = new URL("http://localhost:5555/hello");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");//必须设置为POST方式,而且必须是大写的
connection.setDoInput(true);//因为有输入参数也有输出参数所以都为真
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
OutputStream out = connection.getOutputStream();
//下面替换尖括号是测试传送xml文本字符串测试的
//String str="<list><item><email>aaa!qq</email></item></list>";
//str=str.replaceAll("<","<").replaceAll(">",">");
StringBuilder soap=new StringBuilder();
soap.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://server.hjp.com/\">");
soap.append("<soapenv:Header/>");
soap.append("<soapenv:Body>");
soap.append("<ser:sayHello>");
soap.append("<arg0>aaa</arg0>");
soap.append("</ser:sayHello>");
soap.append("</soapenv:Body>");
soap.append("</soapenv:Envelope>");
String argo=soap.toString();
System.out.println(argo);
out.write(argo.getBytes());//发送SAOP请求
InputStream stream = connection.getInputStream();
byte[] b = new byte[1024];
int len=0;
StringBuffer buffer = new StringBuffer();
while((len=stream.read(b))!=-1){
String s = new String(b, 0, len, "utf-8");
buffer.append(s);
}
System.out.println(buffer.toString());
}
8、请求头和响应头编写及处理返回值是借助SOAPUI工具,界面如下


ajax访问WebService跨域问题的更多相关文章
- Ajax请求WebService跨域问题 [转载]
1.背景 用Jquery中Ajax方式在asp.net开发环境中WebService接口的调用 2.出现的问题 原因分析:浏览器同源策略的影响(即JavaScript或Cookie只能访问同域下的内容 ...
- Ajax请求WebService跨域问题
1.背景 用Jquery中Ajax方式在asp.net开发环境中WebService接口的调用 2.出现的问题 原因分析:浏览器同源策略的影响(即JavaScript或Cookie只能访问同域下的内容 ...
- ajax调用webservice 跨域问题
用js或者jquery跨域调用接口时 对方的接口需要做jsonp处理,你的ajax jsonp调用才可以 egg 接口中已经做了jsonp处理,所以可以跨域调用 //$.ajax({ // url: ...
- ajax 调用webservice 跨域问题
注意两点 1. 在webservice的config中加入这段位置 (注意不是调用webservice的webconfig中加入) <system.webServer> <! ...
- JS-JQuery(JSONP)调用WebService跨域若干技术点
1.JSONP:JSON With Padding,让网页从别的网域获取信息,也就是跨域获取信息,可以当做是一种“工具”,大多数架构Jquery.EXTjs等都支持. 由于同源策略,一般来说位于 se ...
- js中ajax如何解决跨域请求
js中ajax如何解决跨域请求,在讲这个问题之前先解释几个名词 1.跨域请求 所有的浏览器都是同源策略,这个策略能保证页面脚本资源和cookie安全 ,浏览器隔离了来自不同源的请求,防上跨域不安全的操 ...
- Ajax【介绍、入门、解决Ajax中文、跨域、缓存】
什么是Ajax Ajax(Asynchronous JavaScript and XML) 异步JavaScript和XML Ajax实际上是下面这几种技术的融合: (1)XHTML和CSS的基于标准 ...
- 06: AJAX全套 & jsonp跨域AJAX
目录: 1.1 AJAX介绍 1.2 jQuery AJAX(第一种) 1.3 原生ajax(第二种) 1.4 iframe“伪”AJAX(第三种) 1.5 jsonp跨域请求 1.6 在tornad ...
- ajax请求ashx跨域问题解决办法
ajax请求ashx跨域问题解决办法 https://blog.csdn.net/windowsliusheng/article/details/51583566 翻译windowsliusheng ...
随机推荐
- CREATESTRUCT cs 结构体
PreCreateWindow(CREATESTRUCT& cs) typedef struct tagCREATESTRUCT { LPVOID lpCreateParams; // 创建窗 ...
- JQuery控制radio选中和不选中方法总结
一.设置选中方法 代码如下: $("input[name='名字']").get(0).checked=true; $("input[name='名字']"). ...
- 在天河二号上对比Julia,Python和R语言
Julia是一款高级高效为技术计算(technical computing)而设计的编程语言,其语法与其他计算环境类似.其为分布式计算和并行所设计,最知名的地方在于其接近C语言的高效率. 按开发者的话 ...
- QTcpSocket的连续发送数据和连续接收数据
关于这个问题折腾了我好久,以前做些小练习的时候,用QTcpSocket的write()一数据,然后接收方只要emit一个readyread()信号然后就用QTcpSocket的read()去读.本以为 ...
- find & grep 命令 in linux(转)
Linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: -exec,find命令对匹配的文件执 ...
- 自然语言交流系统 phxnet团队 创新实训 项目博客 (十一)
神经网络的计算过程 神经网络结构如下图所示,最左边的是输入层,最右边的是输出层,中间是多个隐含层,隐含层和输出层的每个神经节点,都是由上一层节点乘以其权重累加得到,标上“+1”的圆圈为截距项b,对输入 ...
- php 从1加到100
<?php //1-100利用for循环1-100累加 $sum=0;//初始化sum值为0 for($i=1;$i<=100;$i++)//定义i,循环次数,一般求1-100的和,从1开 ...
- MyBatis SqlSessionDaoSupport实例
在前面的章节中,我们已经讲到了基本的 mybatis 操作,但都是基于 mapper 隐射操作的,在 mybatis3 中这个 mapper 接口貌似充当了以前在ibatis 2中的 DAO 层的作用 ...
- JavaBean递归拷贝工具类Dozer
JavaBean深度拷贝利器——Dozer DozerBeanMapper对象之间相同属性名赋值 DozerBeanMapper + 对象转Map方法 Dozer(JavaBean的映射工具)开发手册
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 De ...