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 ...
随机推荐
- R语言 ggplot2 画平滑图
library(splines) library(ggplot2) dt1 <- structure(list(Age = structure(c(1L, 1L, 1L, 1L, 1L, 1L, ...
- 解决在IE浏览器中JQuery.resize()执行多次的方法(转)
最近在做前台效果的时候用到了JQuery提供的resize()事件.resize 这个事件是监听浏览器窗口的放大与缩小,也就是说浏览器窗口大小的变化. 我在W3CSCHOOL上面查阅的时候,提供了一个 ...
- Oracle 11g 数据库 shutdown 后立即执行 startup mount 报错的解决办法
最新文章:Virson's Blog 今天在配置Goldengate时Capture进程提示未开启归档日志,然后立即用sys用户登录orcl数据库,然后执行了“shutdown immediate”命 ...
- 自然语言交流系统 phxnet团队 创新实训 个人博客 (五)
有关我们这个项目:智能自然语言交流系统,所借鉴的技术有: 第一:我们使用了科大讯飞的在线语音转换,涉及的有文本传给云端服务器的文字转换成语音和本地的语音上传给服务器转换成文字. 涉及的相关的代码有: ...
- 更改虚拟机UUID
如何更改虚拟机UUID?以下方法适用于:Parallels Desktop 10 for MacParallels Desktop 9 for MacParallels Desktop 8 for M ...
- 64位debian系统下安装inodeClient
linux下的inodeClient下载: 链接:http://pan.baidu.com/s/1jIoX6Zk 密码:vnws 里面包括一份说明书,一个32位的,一个64位的文件: 对于64位的客户 ...
- 第三百三十二节,web爬虫讲解2—Scrapy框架爬虫—Scrapy使用
第三百三十二节,web爬虫讲解2—Scrapy框架爬虫—Scrapy使用 xpath表达式 //x 表示向下查找n层指定标签,如://div 表示查找所有div标签 /x 表示向下查找一层指定的标签 ...
- Java如何使用catch来处理异常?
在Java编程中,如何使用catch块来处理异常? 此示例显示如何使用catch来处理异常. package com.yiibai; public class UseOfCatch { public ...
- Android多国语言的value文件夹命名方式
在res目錄下建立不同名稱的values文件來調用不同的語言包Values文件匯總如下:中文(中國):values-zh-rCN中文(台灣):values-zh-rTW中文(香港):values-zh ...
- tracert
TRACERT命令 编辑 Tracert(跟踪路由)是路由跟踪实用程序,用于确定 IP数据包访问目标所采取的路径.Tracert 命令使用用 IP 生存时间 (TTL) 字段和 ICMP 错误消息来确 ...