我正在使用,REMB1.5,,使用grunt-cli,并希望使用dataType: "JSON"实现对CORS支持的ajax调用。
Ember.$.ajax({
type: "GET",
url: App.serverURL + 'logVisit', // callback for jsonP
data : {
'fp': App.fp
},
dataType : "JSON",
success: function(response) {
console.log('DEBUG: visitor has been registered');
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("DEBUG jqXHR.responseText : ",jqXHR.responseText);
var response = jqXHR.responseText;
console.log('Failure!');
if(jqXHR.status&&jqXHR.status==400){
// alert(jqXHR.responseText);
var response = $.parseJSON(jqXHR.responseText);
if (response) {
console.log(response.error);
} else {
// This would mean an invalid response from the server - maybe the site went down or whatever...
console.log("DEBUG: Inside jqXHR.status : Something went wrong");
}
} else {
console.log("DEBUG: Something went wrong");
}
}
});在IE10/11上运行良好。但是在IE8/9上,由于它需要XDR对象,它没有工作,并将控制台显示为
LOG: DEBUG jqXHR.responseText : undefined
LOG: Failure!
LOG: DEBUG: Something went wrong有什么帮助或黑客吗?
我的请求标题:
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36非IE8/9浏览器中的响应头
Access-Control-Allow-Origin:*
Content-Type:application/json
Date:Wed, 25 Mar 2015 16:01:56 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked发布于 2015-03-26 09:21:58
我发现我的回答支持@Isowen的回答。
我在window.App = Ember.Application.create({});之前添加了这段代码,以支持XDR for IE8/9
Ember.$.ajaxTransport( function( options, originalOptions, jqXHR ) {
var xdr;
return {
send: function( _, completeCallback ) {
xdr = new XDomainRequest();
xdr.onload = function() {
if (xdr.contentType.match(/\/json/)) {
options.dataTypes.push("json");
}
completeCallback(200, 'success', { text: xdr.responseText } );
};
xdr.onerror = xdr.ontimeout = function() {
completeCallback(400, 'failed', { text: xdr.responseText } );
}
xdr.open(options.type, options.url);
xdr.send(options.data);
},
abort: function() {
if(xdr) {
xdr.abort();
}
}
};
});根据@Isowen的建议,在执行ajax请求时
Ember.$.ajax({
type: "GET",
url: App.serverURL + 'logVisit',
data : {
'fp': App.fp
},
dataType : "JSON",
xhrFields: {withCredentials: true}, // line added
....
});使用REST适配器处理请求的人员可以使用
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: App.host,
namespace : App.namespace,
ajax: function(url, method, hash) {
hash = hash || {}; // hash may be undefined
hash.crossDomain = true;
hash.xhrFields = { // import line added
withCredentials: true // import line added
};
console.log('DEBUG: inside RESTAdapter ajax call');
return this._super(url, method, hash);
}
});在后端(这里是Spring )
@Component("corsFilter")
public class CORSResponseFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
response.addHeader("Access-Control-Allow-Origin", "http://YOUR-LINK"); // IMPORT LINE ADDED
response.addHeader("Access-Control-Allow-Credentials", "true"); // IMPORT LINE ADDED
if (request.getHeader("Access-Control-Request-Method") != null
&& "OPTIONS".equals(request.getMethod())) {
// CORS "pre-flight" request
response.addHeader("Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE");
response.addHeader("Access-Control-Allow-Headers",
"X-Requested-With,Origin,Content-Type, Accept");
}
filterChain.doFilter(request, response);
}
}谢谢你的帮助。
https://stackoverflow.com/questions/29255559
复制相似问题