gpt4 book ai didi

jquery - 为什么 CORS AJAX 响应的响应 header 为空?

转载 作者:行者123 更新时间:2023-12-01 00:48:39 25 4
gpt4 key购买 nike

使用 jsonp 向外部 Play! 发出 AJAX 请求1.2.4 Heroku上的应用,成功返回数据,在getResponseHeaders()上返回null。

这是我的代码:

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 中看到的响应 header 是:

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

当浏览器可以看到 header 时,为什么使用 getAllReponseHeaders() 返回 null?跨域请求是否会阻止响应 header 发送到 AJAX 响应?

欢迎所有帮助。

谢谢!

最佳答案

恐怕大多数浏览器(Mozilla、Safari、Chrome)尚不支持 Access-Control-Expose-Headers,因此您的 getAllReponseHeaders() 将不工作..

它是在 mozilla Firefox 论坛中作为错误提出的。但最近他们有一个修复程序,您可以查看链接 This

关于jquery - 为什么 CORS AJAX 响应的响应 header 为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12747236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com