用jQuery AJAX调用类方法? [英] Calling a class method with jQuery AJAX?

查看:107
本文介绍了用jQuery AJAX调用类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正从PHP AJAX库xajax移至jQuery AJAX.

使用xajax,我可以通过将公共类方法绑定到javascript函数名称(例如$ this-> registerFunction('javascriptFunctionName',& $ this,'classMethodName'))中,将所有AJAX调用包含在类方法中. /p>

我希望有一种方法可以使用jQuery AJAX做类似的事情,从而可以做类似这样的事情:

$('#myButton').click(function() {
    $.get('class|methodName',
    {
      parameter: value
    },
    function(data) {
      if (data) {
        ...
      }
      else {
        ...
      }
    });

    return false;
  });
});

我知道您可以AJAX调用MVC控制器方法,但是很遗憾,我的旧产品不使用MVC:-(

请告诉我有办法吗?

如果没有,是否可以将调用映射到全局PHP函数?

解决方案

要piggy带什么意思,您可能想制作一个非常简单的ajax控制器并将其所有内容都淘汰掉:

<?php
class AjaxController {

protected $_vars = null;
protected $ajax = null;


public function __construct()
{
  $this->_vars = array_merge($_GET, $_POST);
  $this->_ajax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&  $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
  $this->_response = null;
}

public function process()
{
  if($this->isAjax()){
    if(isset($this->_vars['method']) && isset($this->_vars['class'])
    {
       $callback = array($this->_vars['class'], $this->_vars['method'];
    } elseif(isset($this->_vars['function'])){
      $callback = $this->_vars['function'];
    } 

    if(isset($callback) && is_callable($callback))
    {
       $this->_responseData = call_user_func($callback, $this->_vars);
       $this->sendResponse(200);
    } else {
       $this->_responseData = 'UH OH';
       $this->sendResponse(500);
    }


  }
}

public function sendResponse($httpStatCode = 200){

  // set headers and whatevs
  print $this->_response;
  exit;
}

} // END CLASS

$contoller = new AjaxController();
$controller->process();

您想对课程的参数进行过滤,并可能在htaccess/vhost中进行一些不错的重写,以便可以使用/ajax/class/method之类的URL?您可能希望拥有某种有效的callbak注册表,以便您不只是调用任何有效的php可调用...这可能很危险:-)

I am moving from the PHP AJAX library xajax to jQuery AJAX.

With xajax, I could contain all my AJAX calls inside class methods by binding public class methods to javascript function names (eg. $this->registerFunction('javascriptFunctionName', &$this, 'classMethodName')).

I am hoping there is a way I can do something similar with jQuery AJAX, whereby I can do something like this:

$('#myButton').click(function() {
    $.get('class|methodName',
    {
      parameter: value
    },
    function(data) {
      if (data) {
        ...
      }
      else {
        ...
      }
    });

    return false;
  });
});

I know you can AJAX calls to MVC controller methods, but unfortunately my legacy product doesn't use MVC :-(

Please tell me there is a way?

If not, is there a way to map a call to a global PHP function?

解决方案

To piggyback on what lane is saying you might want to make a really simple ajax controller and rout everything to it:

<?php
class AjaxController {

protected $_vars = null;
protected $ajax = null;


public function __construct()
{
  $this->_vars = array_merge($_GET, $_POST);
  $this->_ajax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&  $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
  $this->_response = null;
}

public function process()
{
  if($this->isAjax()){
    if(isset($this->_vars['method']) && isset($this->_vars['class'])
    {
       $callback = array($this->_vars['class'], $this->_vars['method'];
    } elseif(isset($this->_vars['function'])){
      $callback = $this->_vars['function'];
    } 

    if(isset($callback) && is_callable($callback))
    {
       $this->_responseData = call_user_func($callback, $this->_vars);
       $this->sendResponse(200);
    } else {
       $this->_responseData = 'UH OH';
       $this->sendResponse(500);
    }


  }
}

public function sendResponse($httpStatCode = 200){

  // set headers and whatevs
  print $this->_response;
  exit;
}

} // END CLASS

$contoller = new AjaxController();
$controller->process();

youd want to do some filtering on the params of ofcourse and probably make some nice rewriting in htaccess/vhost so you can use urls like /ajax/class/method?... But you get the general idea. You would probably want to have some kind of valid callbak registry to so youre not just calling any valid php callable... that could be dangerous :-)

这篇关于用jQuery AJAX调用类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆