[英]How to know whether request has come via jQuery $.ajax()
I would like to determine whether or not a request to my REST API has been made from the jQuery $.ajax()
method. 我想确定是否已经从jQuery $.ajax()
方法发出了对我的REST API的请求。
Before answering this question with one of the obvious answers of adding a boolean to the data sent to the server, or by adding a header, I do not want to do this... 在用向发送到服务器的数据中添加布尔值或添加标头的显而易见的答案之一回答这个问题之前 ,我不想这样做...
Obviously this can be achieved like so: 显然,可以这样实现:
$.ajax({
data: {
sentViaAjax: 'true'
}
});
However, for various reasons I would like the boolean/additional header to remain hidden from the code. 但是,由于各种原因,我希望布尔/附加标头在代码中保持隐藏。
Therefore, what I would like to know is does jQuery send any other data in any particular form to the server when it makes a request? 因此,我想知道的是,jQuery在发出请求时会以任何特定形式将其他任何数据发送到服务器吗?
I have tried printing out the $_REQUEST
array but this does not contain anything other than my data sent. 我尝试打印出$_REQUEST
数组,但是除了发送的数据之外,它不包含其他任何内容。 Are there any unique headers to look out for? 是否有任何独特的标题需要注意? If so, are they reliable, as in, will they always be there? 如果是这样,它们是否可靠?例如,它们会一直存在吗?
jQuery sends the following header on non-cross-domain ajax
requests: jQuery在非跨域ajax
请求上发送以下标头:
X-Requested-With: XMLHttpRequest
So you could look for that header on the request. 因此,您可以在请求中查找该标头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.