Frage

I have some controller Ajax. That controller make some validation of request and if it not from ajax returns error message.

Function is_ajax() check header X-Requested-With and return true or false.

I'm use this link from ajax and all work.

/ajax/somecontroller/someaction

When i try use it internal - i have my own validation error - not ajax request.

There is my code:

$deleted = Request::factory("/ajax/somecontroller/someaction")
                        ->headers("Content-Type", "application/x-www-form-urlencoded")
                        ->headers('HTTP_X_REQUESTED_WITH', 'XmlHttpRequest')
                        ->headers('X-Requested-With', 'XmlHttpRequest')                            
                        ->method(Request::POST)
                        ->post(array(
                            "id_zone_comp" => $id_zone_comp
                        ))
                        ->execute()->body();

I send needed heades but have error.

How to send internal request like external?

Kohana 3.2.


Of course I can process internal queries such as Ajax just give them access after is_internal(), but this is not answer.

War es hilfreich?

Lösung

The problem you're facing is related to the fact that it is in fact an internal request. Because of that the headers you're sending are not populating $_SERVER environment info array. They're kept inside $this->request->headers() instead.

The ajax check is done in based on $_SERVER contents, like this:

if (isset($_SERVER['HTTP_X_REQUESTED_WITH']))
{
    // Typically used to denote AJAX requests
    $requested_with = $_SERVER['HTTP_X_REQUESTED_WITH'];
}

The only solution that would not include is_internal() check would be to make this request an external one and to do that you'd have to set the request URL to include protocol (http://) and full domain name - essentially a full address. Then, the request will populate $_SERVER array with new headers and is_ajax() should let it through.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top