开发者

how to call a php class function directly using ajax?

Is it possible to call a php class function DIRECTLY using ajax?

Something like below... except ajax...

myclass::myfunction();

I've been using the jquery library to work with AJAX.

$.get('control.php', {func: funcName, arg1: arg1});

The above is similar to what I'm trying to achieve MINUS the control.php;

I'm not sure if this is even possible, but I just thought it would be nice to skip the landing page (control.php) that recieves the funcName. I have a bunch of conditional statements that sort out what class 开发者_如何学编程function to run based on the funcName recieved.

It seems kind of silly to do this, to have a separate page just to handle function calls.

Is there a better way?


No.

If this were possible, it would be a gaping security hole.


No. You can't invoke a method directly that way.

You could use routing (like the technique used in CodeIgniter and CakePHP) but that's just syntactic sugar that does the same thing -- control your routes to actions.


It is not possible because of a simple reason. How should the AJAX knows, where to find the function. It needs to have a URL to locate the function so it doesn't work without a php file in between.


No for security reasons but there is no reason why you can't do something like this

function run($args){
  //do stuff
}

echo run($_REQUEST);
//or
echo run($REQUEST['name']);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜