H
H
Hazrat Hajikerimov2014-02-11 22:34:12
PHP
Hazrat Hajikerimov, 2014-02-11 22:34:12

How to know that a JSON request has been received in PHP?

Is it possible to find out that an Ajax request has been received to receive JSON.
I probably didn't express myself correctly.

The point is this.
There is a certain catalog, when you enter, you need to make sure that the page opens and the goods are loaded. Here I think is it possible to find out that the server received an ajax request to receive JSON

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Platonov, 2014-02-11
@hazratgs

<?php
if (! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  print "Поступил Ajax-запрос";
}

V
Valery Ryaboshapko, 2014-02-20
@valerium

@dpr gave good advice, but if you really want to hide the kitchen from the user, then why not use the features built into the HTTP protocol?
On the client side
Server side

if ($_SERVER['HTTP_ACCEPT'] == 'application/json') {
    // тут генерируем JSON
} else {
    // тут генерируем HTML
}

The use of the Accept HTTP header is preferred over HTTP_X_REQUESTED_WITH because
  1. you know for sure that you passed the title with handles, and do not rely on browser developers,
  2. use an older and more established (but not deprecated) feature built into the protocol,
  3. leave room for other representations, such as XML, CVS, Plain text, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question