Johnston
Johnston

Reputation: 20884

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. I now have a bunch of unstyled html templates that get their style once they are loaded into the parent template. 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. 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?

Upvotes: 0

Views: 203

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

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?

It depends on how you are making the AJAX requests but most frameworks add some custom header. For example jQuery adds the X-Requested-With: XMLHttpRequest request header on each AJAX request. This header can be used on the server to determine whether it was an AJAX request or not. And if the framework you are using to make an AJAX request doesn't add any custom headers you could add one manually yourself.

Upvotes: 2

Related Questions