#1
  1. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,011
    Rep Power
    534

    Determining if request was via ajax


    99% of my post requests are ajax (and based on the application, I decide whether I wish it to degrade to non-ajax), and other than the initial page load, most of my get requests are as well.

    To allow the server to know if a request was initiated via ajax, it is easy to use client JS to set one of the uploaded values. Is this the best/only way to do so?

    Next, any recommendations on the best/most consistent way to do so? Since for me 99% of the requests are ajax initiated, I am thinking that my PHP application should assume that all requests are ajax unless a field is uploaded with a certain value (and not the more traditional other way around). Opinions?

    Thanks
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    You can't tell whether a request was triggered through the classical browsing workflow, through Ajax or through a monkey typing in raw bytes. It's all just an HTTP message.

    And I wonder why you even ask. Where the request comes from is completely irrelevant for your server. All it's supposed to do is send data in response to an HTTP request.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,011
    Rep Power
    534
    Originally Posted by Jacques1
    You can't tell whether a request was triggered through the classical browsing workflow, through Ajax or through a monkey typing in raw bytes. It's all just an HTTP message.
    Sure I could. Which one do you think was sent via Ajax?

    Code:
    Array
    (
        [sentByAjax] => 1
        [data] => MyData
        [submit] => SUBMIT
    )
    
    Array
    (
        [sentByAjax] => 0
        [data] => MyData
        [submit] => SUBMIT
    )
    Originally Posted by Jacques1
    And I wonder why you even ask. Where the request comes from is completely irrelevant for your server. All it's supposed to do is send data in response to an HTTP request.
    Request comes in, and I do the work. Then if the request came via Ajax, I return some JSON, and if classic browsing workflow, display a page or redirect. Better way to do this?
  6. #4
  7. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,112
    Rep Power
    487
    If I may say here, your question was answered by Jacques1 but by using your method (i.e. including a GET or POST parameter to say that it came from a certain source) it is possible.

    I think the answer to your question "is there a better way to do this", is probably going to be "no - see Jacques1's earlier reply".

    I also think you've answered your own other q: if 99% of your application requests come via ajax then yes, common sense says to treat it as such by default.
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,011
    Rep Power
    534
    Originally Posted by badger_fruit
    If I may say here, your question was answered by Jacques1....

    ....is probably going to be "no - see Jacques1's earlier reply".
    I don't see it. I understood him to say that one could not tell whether a request was triggered through the classical browsing workflow or through Ajax, and that there is never a reason to try to tell in the first place.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by NotionCommotion
    Which one do you think was sent via Ajax?
    The data says that is has been sent via Ajax. That doesn't mean it actually has.

    If you have a bug in your applications, you may get sentByAjax = 1 for classical requests or sentByAjax = 0 for Ajax requests. Or maybe the user messes with the data.

    Of course you can set an Ajax flag in the request data. But this has nothing to do with “determining if the request was via Ajax”. It's just data.



    Originally Posted by NotionCommotion
    Request comes in, and I do the work. Then if the request came via Ajax, I return some JSON, and if classic browsing workflow, display a page or redirect. Better way to do this?
    The idea isn't wrong, it's just bad wording, I guess. The original post sounded like you actually wanna find out the origin of the request. But in reality, you just wanna serve different data depending on whether or not a certain flag in the input data is set.

    Of course you can do this. If the HTML response and the JSON response reflect the same content, it makes perfect sense to use the same URL. But it's not really Ajax-related. You just serve the same content in different formats (which may be used for all kinds of things).
    Last edited by Jacques1; January 30th, 2014 at 07:48 AM.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2003
    Posts
    804
    Rep Power
    101
    Originally Posted by NotionCommotion
    Request comes in, and I do the work. Then if the request came via Ajax, I return some JSON, and if classic browsing workflow, display a page or redirect. Better way to do this?
    What you are describing is the responseType. If your AJAX request needs to get JSON data, then set the responseType "json" and at the server you'll use that header to determine whether to send JSON or else send a page or a redirect.
    Last edited by Clone53421; February 4th, 2014 at 02:42 PM.

IMN logo majestic logo threadwatch logo seochat tools logo