- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的ajax请求:
$.post('server.php',data, function (json) {console.log(json)},'json');
我已经将其设置为 jQuery 需要最后一个 dataType
设置指定的 json。
相关问题:dataType
是 responseType
的另一个名称吗?请参阅http://msdn.microsoft.com/en-us/library/windows/apps/hh871381.aspx
现在是我真正的问题。 server.php 有时返回 json,有时返回 html,有时返回 xml,并且在渲染响应时会设置适当的 header 。
但是,在呈现响应之前,服务器需要确定要提供的数据类型。我希望响应基于从客户端接收到的 dataType
和/或 responseType
header 。如何在 PHP 中读取此 header ?
最佳答案
XMLHttpRequest.responseType
属性不以任何形式发送到服务器的信息。因此,您不能使用它来让服务器的 php 代码确定请求的内容类型。
但是有一个名为 ACCEPT
的 HTTP 请求 header 字段(请参阅 mdn ),可以使用 XMLHttpRequest.setRequestHeader()
设置(再次参见 mdn)。有了它,您可以将一些信息归因于请求,这些信息可以通过您的 php 代码通过 $_SERVER['HTTP_ACCEPT']
访问。
在客户端,您可以执行此操作来代替(或补充)设置 XMLHttpRequest.responseType
// create XHR Object
var xhr = new XMLHttpRequest();
// open request
xhr.open(<url to request>, "POST");
// set the responseType to json (which only provokes
// that the returned data is automatically interpreted
// by the browser as being json. saving you from manually
// doing a JSON.parse(xhr.responseText);
xhr.responseType='json';
// set the request HTTP ACCEPT header to be the MIME of JSON
// that is "application/json"
xhr.setRequestHeader("ACCEPT","application/json");
//[... setup the callbacks for handling the resonses or failures]
//send the request
xhr.send();
在服务器端,您可能会执行以下操作
if($_SERVER['HTTP_ACCEPT'] === 'application/json')
{
header('Content-Type: application/json');
echo '<json-string>';
die(0);
}
else
{
// do some stuff for other Request that do
// not want explicitly json.
}
关于javascript - PHP判断客户端请求的dataType或responseType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21854779/
这个问题已经有答案了: EmailAddress or DataType.Email attribute (3 个回答) 已关闭 3 年前。 我使用了像[EmailAddress]这样的数据属性和[D
我收到此错误: "CS0104: 'DataType' is an ambiguous reference between 'System.ComponentModel.DataAnnotations
1)我目前正在尝试理解以下代码,但我无法理解 void(*func)(void) 的含义,我可以理解我正在尝试保存名为“function”的函数的地址来自 list0513,在 void 指针函数处,
我们可以以某种方式应用这种格式吗? [Display(Name = "Date of Birthday")] [DataType(DataType.Date)] public DateTime DOB
我已按照 ORMLite 文档的规定将我的类型声明为 SERIALIZABLE,但我仍然得到: ORMLite can't store unknown class interface java.io.
嗨。 这就是我想要做的: str2 = "91"; str1 = "19"; var testQuery = from c1 in str1
我在 MVC 5 中使用 ASP.NET 身份。对于密码验证,我在 AccountViewModels.cs 中有以下代码: [Required] [DataType(DataType.Passwor
我试图了解使用之间的主要区别是什么[DataType(DataType.EmailAddress)] & [EmailAddress] . 在模型类中:- public class MYViewMod
使用 DataType Attribute 有什么区别?并传入值 DataType.Phone和 Phone Attribute它继承自 DataType 并自动设置 DataType.Phone?
我的 ViewModel 中有以下字段: [DataType(DataType.Date)] [Display(Name = "Preferred date)")] publi
如果没记错的话,是[DataType(DataType.Currency)]的DataAnnotation的对应部分在 Fluent api 中是 modelBuilder.Entity.Proper
给定一个 pandas.DataFrame带有包含混合数据类型的列,例如 df = pd.DataFrame({'mixed': [pd.Timestamp('2020-10-04'), 999, '
我在电子邮件字段上使用此代码: [Required] [DataType(DataType.EmailAddress)] [Display(Name = "Email addr
我是 NodeJs 开发的新手 我正在使用带有 mysql 和 Sequelize 的 NodeJs 来创建具有这些属性的 Batch 模型。 const Batch = sequelize.defi
我想动态生成我的数据框架构我有以下内容 错误: assert isinstance(dataType, DataType), "dataType should be DataType" Asse
我是 Golang 的新手。抱歉,我仍然对以下两者之间的区别感到困惑: type 和 type = 这是一个例子: package main import "fmt" func main()
当电子邮件地址以数字开头时,DataType DataAnnotation 中 DataType.Email 的内部验证似乎失败了。当具有 1234Something@gmail.com 样式电子邮件
有需求从CSV文件中提取数据,然后将其转换为XML,最终加载到目标中。 CSV文件中的数据为 Number,Email,Email Communication,Member Type,VIN 8532
我正在使用 [DataType(DataType.Date)]关于我们模型中的属性。 如何让 Razor 添加 css 类 datepicker到所有属于 DataType.Date 的文本框特性?
我的表 CalTime 有一个时间数据类型的列 Timespent。它以 HH:MM:SS 格式存储值。请建议如何求和该列的值。 Timespent _______ 00:07:
我是一名优秀的程序员,十分优秀!