jquery getResponseHeader 总是返回“未定义"?

2023-05-14前端开发问题
38

本文介绍了jquery getResponseHeader 总是返回“未定义"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个通过 ajax 提交的表单.我正在使用 jquery 表单插件.我想要做的是获取从我的服务器返回的位置"标头.我可以在萤火虫中看到它.但是每当我在成功回调中调用 getResponseHeader() 函数时,它总是返回未定义"..

I have a a form that I am submitting via ajax. I am using the jquery form plugin. What I am trying to do is get the 'Location' header which is returned from my server. I can see it in firebug. But whenever I call the getResponseHeader() function in my success callback, it always returns 'undefined'..

代码:

form.ajaxForm({
  dataType: 'xml',
  data: {format: 'xml'},
  resetForm: true,
  success: function(xml,status,xhr){
    var location = xhr.getResponseHeader('Location');
    alert(location);
  });

位置未定义.但我可以在萤火虫中看到位置"标题.我错过了什么?即使我从 xhr 对象调用 getAllResponseHeaders(),它也会返回 'undefined'

location is undefined. But I can see the 'Location' header in firebug. What am I missing? Even if I call getAllResponseHeaders() from the xhr object, it returns 'undefined'

推荐答案

如果这是 CORS 请求,你可能会在调试工具(例如 Chrome->Inspect Element->Network)中看到所有标头,但 xHR 对象只会检索标头(通过 xhr.getResponseHeader('Header')) 如果这样的标头是简单响应标头:

If this is a CORS request, you may see all headers in debug tools (such as Chrome->Inspect Element->Network), but the xHR object will only retrieve the header (via xhr.getResponseHeader('Header')) if such a header is a simple response header:

  • 内容类型
  • 最后修改
  • 内容语言
  • 缓存控制
  • 过期
  • 语用

如果不在此集合中,则它必须存在于 Access-Control-Expose-Headers 服务器返回的标头.

If it is not in this set, it must be present in the Access-Control-Expose-Headers header returned by the server.

关于所讨论的情况,如果它是一个 CORS 请求,则只有当且仅当,才能通过 XMLHttpRequest 对象检索 Location 标头下面的标题也存在:

About the case in question, if it is a CORS request, one will only be able to retrieve the Location header throgh the XMLHttpRequest object if, and only if, the header below is also present:

Access-Control-Expose-Headers: Location

如果它不是 CORS 请求,XMLHttpRequest 将没有问题检索它.

If its not a CORS request, XMLHttpRequest will have no problem retrieving it.

这篇关于jquery getResponseHeader 总是返回“未定义"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

Rails 3.1 ajax:成功处理
Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)...
2024-04-20 前端开发问题
11

ExecJS::ProgramError: SyntaxError: 保留字“function"
ExecJS::ProgramError: SyntaxError: Reserved word quot;functionquot;(ExecJS::ProgramError: SyntaxError: 保留字“function)...
2024-04-20 前端开发问题
13