0

我有一些控制器 Ajax。该控制器对请求进行一些验证,如果不是来自 ajax,则返回错误消息。

函数 is_ajax() 检查标头 X-Requested-With 并返回 true 或 false。

我正在使用来自 ajax 的这个链接并且所有的工作。

/ajax/somecontroller/someaction

当我尝试在内部使用它时 - 我有自己的验证错误 - 而不是 ajax 请求。

有我的代码:

$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();

我发送所需的标题但有错误。

如何像外部一样发送内部请求?

小花 3.2.


当然,我可以处理诸如 Ajax 之类的内部查询,只需在 is_internal() 之后授予它们访问权限,但这不是答案。

4

1 回答 1

1

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.

于 2013-02-12T12:58:20.677 回答