Javascript SmartGWT数据源和CORS
我正在使用最新的Spring 4.1.5创建一个RESTful后端。web服务单元经过测试,工作正常。我为他们实现了SimpleCorsFilter,使它们跨域工作Javascript SmartGWT数据源和CORS,javascript,spring,rest,cors,smartgwt,Javascript,Spring,Rest,Cors,Smartgwt,我正在使用最新的Spring 4.1.5创建一个RESTful后端。web服务单元经过测试,工作正常。我为他们实现了SimpleCorsFilter,使它们跨域工作 public class SimpleCORSFilter implements Filter { public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletExce
public class SimpleCORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
$("#retrieve2").click(function()
{
var xhr = new XMLHttpRequest();
xhr.open('GET','http://127.0.0.1:8080/urm-ws-0.0.1-SNAPSHOT/rest/login/user/tholmes/pwd/mypwd');
xhr.onreadystatechange = function()
{
if (this.status == 200 && this.readyState == 4)
{
console.log('response: ' + this.responseText);
}
};
xhr.send();
});
$("#retrieve3").click(function()
{
$.ajax(
{
url : "http://localhost:8080/urm-ws-0.0.1-SNAPSHOT/rest/login/user/tholmes/pwd/mypwd"
})
.then(
function(data) {
$('#userId').append(data.userId);
$('#username').append(data.username);
});
});
在使用SmartGWT RESTDataSource测试这些web服务之前,我首先使用一些JavaScript进行测试。我有三种测试方法。
第一种方法基于StackOverflow的另一个链接,它表明这是一个很好的测试,可以确保SmartGWT数据源工作正常。
当我从浏览器测试这段代码时,我得到了一个跨脚本错误
$("#retrieve1").click(function()
{
$.ajax({
type : "GET",
contentType : "application/json",
url : "http://127.0.0.1:8080/urm-ws-0.0.1-SNAPSHOT/rest/login/user/tholmes/pwd/mypwd",
dataType : "json",
crossDomain : true,
success : function(data, status,xhr) {
$("#content").text();
},
error : function(xhr,status, error) {
$("#content").text("Unable to retrieve data");
}
});
});
然而,接下来的两种方法对于恢复数据非常有效。因此,在本例中,我确信web服务将跨域工作
public class SimpleCORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
$("#retrieve2").click(function()
{
var xhr = new XMLHttpRequest();
xhr.open('GET','http://127.0.0.1:8080/urm-ws-0.0.1-SNAPSHOT/rest/login/user/tholmes/pwd/mypwd');
xhr.onreadystatechange = function()
{
if (this.status == 200 && this.readyState == 4)
{
console.log('response: ' + this.responseText);
}
};
xhr.send();
});
$("#retrieve3").click(function()
{
$.ajax(
{
url : "http://localhost:8080/urm-ws-0.0.1-SNAPSHOT/rest/login/user/tholmes/pwd/mypwd"
})
.then(
function(data) {
$('#userId').append(data.userId);
$('#username').append(data.username);
});
});
我不是对JavaScript非常了解,我想知道这三种方法之间的区别是什么?由于某种原因,第一种方法不起作用,所以我不知道是否需要修复该代码,还是需要修复该JavaScript方法中的代码
最终,我希望让我的web服务与SmartGWT数据源一起工作。我知道这是可以做到的,我觉得我就在那里。
如果我需要提供更多的信息,请告诉我。感谢您的帮助。在第一种情况下,浏览器会发送预飞的选项请求,因为应用程序/json内容类型。我想这个请求在服务器上没有得到正确处理。有关这一差异的更多信息,请参见我的部分答案是: 他们的CORS过滤器和我的略有不同。 我有以下几点:
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
它确实需要:
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, Content-Type, If-Modified-Since");
一旦我添加了这个,第一个javascript方法就起作用了,一旦它起作用了,我就可以测试我的SmartGWT数据源,它可以与我的远程RESTful web服务一起工作。第一个案例有什么错误?是Firefox浏览器说有一个跨源脚本调用被阻止了。但是,我刚刚解决了这个问题,所以我想我现在已经准备好了。我有一个开始工作,所以现在我必须尝试、放置、发布和删除。