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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看