使用jsonp將AJAX請求發送到外部Play! 1.2.4在Heroku上成功返回數據的應用程序,在getResponseHeaders()
上返回null。爲什麼CORS AJAX響應中的響應標頭爲空?
這裏是我的代碼:
var ajaxresult;
ajaxresult = $.ajax({
'complete': function (jqXHR, status) {
console.log('Complete!');
console.log(status);
console.log("on compelte: " + jqXHR.getAllResponseHeaders());
},
'dataType': "jsonp",
'error': function (jqXHR, status, error) {
console.log('Error!');
console.log(status);
console.log(error);
console.log("on error: " + jqXHR.getAllResponseHeaders());
},
'success': function (data, status, jqXHR) {
console.log('Success!');
console.log(status);
console.log(data);
console.log("on success: " + ajaxresult.getAllResponseHeaders());
console.log("on success(2): " + jqXHR.getAllResponseHeaders());
},
'type': 'GET',
'url': url + "findAllSpecials"
});
如果我傾倒jqXHR的內容,我得到:
"readyState: 4","setRequestHeader: function(a,b){if(!s){var c=a.toLowerCase();a=m[c]=m[c]||a,l[a]=b}return this}","getAllResponseHeaders: function(){return s===2?n:null}","getResponseHeader: function(a){var c;if(s===2){if(!o){o={};while(c=bG.exec(n))o[c[1].toLowerCase()]=c[2]}c=o[a.toLowerCase()]}return c===b?null:c}","overrideMimeType: function(a){s||(d.mimeType=a);return this}","abort: function(a){a=a||\"abort\",p&&p.abort(a),w(0,a);return this}","done: function(){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this}","fail: function(){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this}","progress: function(){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this}","state: function(){return e}","isResolved: function(){return!!i}","isRejected: function(){return!!i}","then: function(a,b,c){i.done(a).fail(b).progress(c);return this}","always: function(){i.done.apply(i,arguments).fail.apply(i,arguments);return this}","pipe: function(a,b,c){return f.Deferred(function(d){f.each({done:[a,\"resolve\"],fail:[b,\"reject\"],progress:[c,\"notify\"]},function(a,b){var c=b[0],e=b[1],g;f.isFunction(c)?i[a](function(){g=c.apply(this,arguments),g&&f.isFunction(g.promise)?g.promise().then(d.resolve,d.reject,d.notify):d[e+\"With\"](this===i?d:this,[g])}):i[a](d[e])})}).promise()}","promise: function(a){if(a==null)a=h;else for(var b in h)a[b]=h[b];return a}","success: function(){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this}","error: function(){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this}","complete: function(){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this}","statusCode: function(a){if(a){var b;if(s<2)for(b in a)j[b]=[j[b],a[b]];else b=a[v.status],v.then(b,b)}return this}","status: 200","statusText: success"]
在Opera和Firefox的響應頭:
HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Type: text/plain; charset=utf-8
Server: Play! Framework;1.2.4;prod
Set-Cookie: PLAY_FLASH=;Expires=Fri, 5-Oct-12 12:47:15 GMT;Path=/
Set-Cookie: PLAY_ERRORS=;Expires=Fri, 5-Oct-12 12:47:15 GMT;Path=/
Set-Cookie: PLAY_SESSION=...
Content-Length: 1290
Connection: keep-alive
爲什麼是使用getAllReponseHeaders()
返回null時,瀏覽器可以看到頭?跨域請求是否阻止將響應頭髮送到AJAX響應?
歡迎任何幫助。
謝謝!
你可以用一個例子證明這一點? –
這取決於您的服務器語言。如果你使用的是Java服務器,你可以編寫一個servlet過濾器來添加這樣的頭文件:'response.addHeader(「Access-Control-Allow-Headers」,「User-Agent,Content-Type,*」); 。如果你不想編寫你自己的過濾器,你可以使用[ebay的這個實現](https://github.com/eBay/cors-filter)並配置它。 – andrecardoso
感謝您發佈andrecardoso。我其實已經解決了我的問題。你可以看看我在這張票上的評論:http://stackoverflow.com/questions/3136140/cors-not-working-on-chrome/3600086#comment30368211_3600086 –