duanchuli5647 2013-09-07 09:04
浏览 52
已采纳

使用javascript / jQuery在zend模型中调用php方法的最佳方法是什么

I'm developing a website where i have to communicate with web services. I’m using Ajax calls like this, for instance if I want to call a method called getCustomer(...) I have in my Zend project a module called "customer".

Inside of my controller, I have an action called "jsonAction"

public function jsonAction() {
        $this->_helper->viewRenderer->setNoRender();
        $server = new Zend_Json_Server();
        $server->setClass('Customer_Model_Customer');
        $server->handle();
        exit;
    }

Them in my model I have the function:

/**
 * Get Customer
 * @param string $customerNr
 * @return object
 */
public function getCustomer($customerNr){
    /*....*/
}

So I want to call this function using Ajax calls. What i did was something like this:

getCustomer : function(customerNr){
        if(customerNr == null)
            return;
        var request                 = {};
        request.method              = "getCustomer";
        request.params              = {};
        request.params.customerNr   = customerNr;
        request.id                  = Math.floor(Math.random()*101);
        postObject(webServicesURL, JSON.stringify(request), successGetCustomer);
    },

Where postObject it’s the Ajax function:

function postObject(url, request, successCallback){    
    try{
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: url,
            contentType: "application/x-www-form-urlencoded",
            data: request,
            xhrFields: {
                withCredentials: true
            },
            success: function(data){
                successCallback(data);
            },
            error:  function (data) {
                /*Notify error*/
            }
        });
    } catch(ex){
        /*erro*/
    }
}

My question is, there is another way to do this? a best and elegant way? I'm new in web developing and that’s why i'm asking for your help.

Note: I'm using php Zend framework 1.12, Ember 1.0 and JQuery 1.8

  • 写回答

3条回答 默认 最新

  • dss67853 2013-09-07 10:32
    关注

    A better approach could have been not to call a single action rather calling different action and let contextswitch to expose various data type you can find a code snippet here

    Here is an approach

    //put this in your bootstrap
      protected function _initContextSwitch()
      {
        // Initialize contextSwitch helper
        Zend_Controller_Action_HelperBroker::addHelper(new Custom_Action_Helper_ContextSwitch());
      }
    
    class Custom_Action_Helper_ContextSwitch extends
      Zend_Controller_Action_Helper_ContextSwitch
    {
      public function preDispatch()
      {
          $actionName = $this->getActionController()->getRequest()->getActionName();
          $this
            ->addActionContext($actionName, 'json')
            ->initContext();
      }
    }
    

    Now extend Zend_Controller_Action_Helper_ContextSwitch to make all action to expose json data

    Now call every method with query string(format=json) like http://example.com/module/controller/action?format=json this and will expose json data for every action

    N.B Remember this is a custom action helper which namespace is Custom

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器