javascript 如何获取“getJSON”响应标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15385514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get 'getJSON' response header
提问by Espanta
I need to access the size of response message I am getting from another machine (cross-domain request) using $.getJSON, although I can see the request and response in chrome console, it does not work. Here is my request code:
我需要使用 $.getJSON 访问我从另一台机器(跨域请求)获得的响应消息的大小,虽然我可以在 chrome 控制台中看到请求和响应,但它不起作用。这是我的请求代码:
xhr=$.getJSON('http://192.168.1.102/server/server.php?callback=?',
{data:array}, function(res){
alert(xhr.getAllResponseHeader());
},
type='json');
when running I get "Uncaught TypeError: Object # has no method 'getAllResponseHeader' " error. When I use
运行时出现“Uncaught TypeError: Object # has no method 'getAllResponseHeader'”错误。当我使用
alert(xhr.getResponseHeader("Content-Length"));
I get "null".
我得到“空”。
Please consider that I am using cross-domain get.
请考虑我正在使用跨域获取。
采纳答案by Espanta
I observed many visits to this question and thought it is good to provide the solution to this problem. Unfortunately none of the offered solutions worked and I did two things to solve the problem.
我观察了很多次访问这个问题,并认为提供这个问题的解决方案是好的。不幸的是,提供的解决方案都没有奏效,我做了两件事来解决问题。
I verified that I have the following codes right after the
<?php
tag in my server side:header("Content-Type: application/*"); header("Access-Control-Allow-Origin :*"); header("Access-Control-Allow-Methods: POST, GET, OPTIONS"); header("Access-Control-Max-Age: 1000"); header("Access-Control-Allow-Headers: Content-Type");
I slightly changed the way I do the call as follows and it always works for me.
function name(){ $.ajax({ type: 'POST', crossDomain: true, data: { data1: data }, dataType: 'text', error: function (res, textStatus, errorThrown) { alert('Connection Terminated. Please try again'); }, success: function(res, textStatus, jqXHR) { //Process the result in res; }, });//ajax }//function name
我确认我
<?php
在服务器端的标签之后有以下代码:header("Content-Type: application/*"); header("Access-Control-Allow-Origin :*"); header("Access-Control-Allow-Methods: POST, GET, OPTIONS"); header("Access-Control-Max-Age: 1000"); header("Access-Control-Allow-Headers: Content-Type");
我稍微改变了我打电话的方式,如下所示,它总是对我有用。
function name(){ $.ajax({ type: 'POST', crossDomain: true, data: { data1: data }, dataType: 'text', error: function (res, textStatus, errorThrown) { alert('Connection Terminated. Please try again'); }, success: function(res, textStatus, jqXHR) { //Process the result in res; }, });//ajax }//function name
回答by vhtc
Do not use JSONP, it isn't really a cross domain request (JSONP explained), it's a hack that only works for GET requests, whereas AJAX allows any http method.
不要使用 JSONP,它不是真正的跨域请求(JSONP 解释),它是一种仅适用于 GET 请求的黑客,而 AJAX 允许任何 http 方法。
Try preparing your server to allow cross domain requests (more details) and doing this:
尝试准备您的服务器以允许跨域请求(更多详细信息)并执行以下操作:
$.ajax({
type: "get",
url: "http://192.168.1.102/server/server.php",
crossDomain: true,
cache: false,
dataType: "json",
contentType: "application/json; charset=UTF-8",
data: array,
success: function(data, textStatus, xhr) {
console.log(data);
console.log(xhr.getResponseHeader("Content-Length"));
},
error: function (xhr, textStatus, errorThrown) {
console.log(errorThrown);
}});
Thereby, the xhr object is set and you can access it's header.
因此,设置了 xhr 对象,您可以访问它的标题。
回答by Rohan Kumar
Try this:
试试这个:
xhr=$.getJSON('http://192.168.1.102/server/server.php?callback=?',
{data:array}, function(res,status,xhr){
alert(xhr.getAllResponseHeaders());
// not getAllResponseHeader its only getResponseHeader
});
For cross domain
use
供cross domain
使用
$.ajax({
url: 'http://192.168.1.102/server/server.php?callback=?',
dataType: 'json',
jsonpCallback: 'MyJSONPCallback', // specify the callback name if you're hard-coding it
success: function(data){
// we make a successful JSONP call!
}
});
Refer this jQuery getJSON works locally, but not cross domain
参考这个jQuery getJSON 可以在本地工作,但不能跨域
Docs For getAllResponseHeaders
, getResponseHeader
and ajax
http://api.jquery.com/jQuery.ajax/
文档getAllResponseHeaders
,getResponseHeader
以及ajax
http://api.jquery.com/jQuery.ajax/