$this->_request->isXmlHttpRequest() 不起作用

$this->_request->isXmlHttpRequest() Not Working

本文关键字:不起作用 isXmlHttpRequest this- request-      更新时间:2023-09-26

我正在研究对我来说是新的ZEND框架,AJAX也是如此。我尝试了很多例子,但没有一个有效。

请告诉我我在这个代码中错了。

首页.phtml

<select name="year" onchange="saveChanges(this);">
                                    <option value="najam">Najam</option>
                                    <option value="Ali">Ali</option>
                                    <option value="Hassan">Hassan</option>
                                    <option value="Hassam">Hassam</option>
                                </select>
                                <script type="text/javascript">
                                    function saveChanges(object){   
    $.ajax({
        method :'POST',
        url: 'home',
        data: object.value,
        cache: false,
        error: function(e){
            alert(e);
            console.log("error" + e);
        },
        success: function(response){
            // A response to say if it's updated or not
            alert("Success" + object.value);
            console.log("Success");
        }
    });   
}
                                </script>

主页行动

if($this->_request->isXmlHttpRequest()){
//do this
}
else {
//do this
// It always in the else Part.

}

尽管它在控制台和 javascript 警报消息上打印成功消息。

如果此代码有误,请帮助我

method :'POST'

type: "POST"

我总是使用$this->_request->isXmlHttpRequest()来检测 ajax 请求并且始终有效。

对不起,我的英语说得不好

更新:

控制器:

final class IndexController extends Zend_Controller_Action
{
    final public function indexAction(){
        if($this->_request->isXmlHttpRequest()){
            header('Content-type: application/json;charset=UTF-8');
            exit(json_encode(($this->_request->getPost())));
        }
    }      
}

视图:

                            <select id="year" name="year">
                                <option value="najam">Najam</option>
                                <option value="Ali">Ali</option>
                                <option value="Hassan">Hassan</option>
                                <option value="Hassam">Hassam</option>
                            </select>
                            <script type="text/javascript">
                                $(function(){
                                   $("#year").change(function(){
                                       var _this = $(this);
                                        $.ajax({
                                            type :'POST',
                                            url: "", // empty string means current URL
                                            data: {year: _this.attr("value")},
                                            dataType: 'json',
                                            cache: false,
                                            error: function(e){
                                                console.log(e);
                                            },
                                            success: function(response){
                                                console.log(response);
                                            }
                                        });   
                                    });
                                });
                            </script>

以下是源代码:

http://www.mediafire.com/?hvv4830macfl0wm

希望有帮助!

在 Zend Framework 中,你可以为你的操作添加一个ContextSwitch。根据上下文,操作将在相关模板中呈现。

例如

    public function init()
    {
        $contextSwitch = $this->_helper->getHelper('contextSwitch');
        $contextSwitch->addActionContext('list', 'xml')
                      ->initContext();
    }

在您的情况下

    $contextSwitch->addActionContext('home', 'ajax')

看这里 http://framework.zend.com/manual/1.8/en/zend.controller.actionhelpers.html

编辑:这是针对ZF1的,如果您使用的是ZF2,您可能需要研究View Strategyhttp://framework.zend.com/manual/2.0/en/modules/zend.view.quick-start.html#creating-and-registering-alternate-rendering-and-response-strategies