Welcome Guest, Not a member yet? Register   Sign In
How to determine request method when using JQuery
#1

[eluser]Nicholas Hanks[/eluser]
Is there any way to determine whether request is via AJAX or regular post when using JQuery.
I know there is one in Rails like respond_to do |format|

So, I was wondering whether there is anything similar like this in CodeIgniter or is there any way to achieve this in CodeIgniter. I will really appreciate any help.
#2

[eluser]Johan André[/eluser]
I found this on some blog, can't really remember where.

Code:
function is_ajax() {
    return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
}

Use it in controller to check if the request is done via XMLHttp. Works like a charm.
#3

[eluser]Colin Williams[/eluser]
That is right, Johan. Some obscure server configs might not hold that environment variable, but most do, and so it is a reliable check for requests made via Ajax (and I'd imagine other unique request types [e.g. from server, from browser, from flash, etc])
#4

[eluser]Johan André[/eluser]
Actually, it just struck me you may want to use CIConfused built-in class to check the server-variables... Works this way too ofcourse...
#5

[eluser]RaF007[/eluser]
[quote author="Johan André" date="1233081948"]Actually, it just struck me you may want to use CIConfused built-in class to check the server-variables... Works this way too ofcourse...[/quote]

Johan, could you elaborate? Which class are we talking about?
#6

[eluser]Johan André[/eluser]
[quote author="RaF007" date="1233085393"]Johan, could you elaborate? Which class are we talking about?[/quote]

Ofcourse:

You can use the (preloaded) input-class to get $_SERVER variables:

Code:
$this->input->server('some_data');

Not that it really matters for the functionality (in this case).
The great things about the input-class is that it test if the key is set before returning the data. If not set it returns false.
#7

[eluser]Nicholas Hanks[/eluser]
Thanks a lot for your reply.




Theme © iAndrew 2016 - Forum software by © MyBB