• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Detecting an Ajax request

 
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is there a way in my servlet to check if a request came from an ajax call (XMLHTTP request) and not from the usual
HTTP request?

I want to add logic in my filter such that if the request is an ajax call, i would follow a different route of processing.

My idea is like this:



Thanks.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is nothing different from an Ajax call compared to a normal http call. BUT you can make it different yourself by added a header, sending up an extra parameter, etc.

Eric
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can check to see if the header X-Requested-With is set to XMLHttpRequest*, or use a custom header as Eric suggested.



* Check that this works for all browsers that you need to support. Works fine in Firefox, Safari and Chrome. I don;t know about IE because we do not support it with our web app.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, please try to use better subjects. There are a lot of Ajax questions in this forum. I've adjusted it to something more meaningful.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ugh, and consolidate your questions, since this is undoubtedly related to the previous filter question I answered.
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks I think I will go with adding a query parameter in all my ajax call. In this manner I think I can tell my
spring interceptor to handle ajax call differently



I don;t know about IE because we do not support it with our web app.


I wish I had the luxury of having this. But apparently most of my users are using IE 6 since this would be an intranet app.

Ugh, and consolidate your questions, since this is undoubtedly related to the previous filter question I answered


Hi David, sorry... I am a bit excited with this ajax/jquery thing. I knew ajax and jquery are not new but they are to me right now...
My boss gave me a lot of SWT desktop projects for the last two years...
reply
    Bookmark Topic Watch Topic
  • New Topic