简体   繁体   English

如何检测GET请求是来自AJAX请求还是浏览器请求?

[英]How to detect if GET request comes from an AJAX request or browser request?

I am writing an app using requireJS and the text.js extension. 我正在使用requireJS和text.js扩展名编写应用程序。 I now have a bunch of unstyled html templates that get their style once they are loaded into the parent template. 我现在有一堆没有样式的html模板,一旦加载到父模板中就可以获得它们的样式。 Everything works fine. 一切正常。 The only issue I have is that users can technically visit the url of the template and see an unstyled html page. 我唯一的问题是用户可以从技术上访问模板的网址,并看到一个没有样式的HTML页面。 Is there anyway to detect that these requests come from an AJAX request invoked by javascript vs a request that came directly from the user using a browser? 反正有没有检测到这些请求来自javascript调用的AJAX请求与使用浏览器直接来自用户的请求?

Is there anyway to detect that these requests come from an AJAX request invoked by javascript vs a request that came directly from the user using a browser? 反正有没有检测到这些请求来自javascript调用的AJAX请求与使用浏览器直接来自用户的请求?

It depends on how you are making the AJAX requests but most frameworks add some custom header. 这取决于您如何制作AJAX请求,但大多数框架都添加了一些自定义标头。 For example jQuery adds the X-Requested-With: XMLHttpRequest request header on each AJAX request. 例如,jQuery在每个AJAX请求上添加了X-Requested-With: XMLHttpRequest请求标头。 This header can be used on the server to determine whether it was an AJAX request or not. 可以在服务器上使用此标头来确定它是否是AJAX请求。 And if the framework you are using to make an AJAX request doesn't add any custom headers you could add one manually yourself. 如果您用于发出AJAX请求的框架没有添加任何自定义标头,您可以自己手动添加一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM