dongraa1986 2013-05-21 12:14
浏览 57

使用Ajax用参数调用php函数

First off - NO JQUERY. I hate that thing with a passion and I can't possibly imagine how a native code implementation would be less efficient.

What I am after is a way to call any PHP function from Javascript and pass parameters to the function using call_user_func_array. I have written this exact code before but can't think of how I did it.

In the end, I want to be able to (in JS) be able to do something like:

var responseString = callPhpFunction(func, param1, param2, etc);

`

Which is an ideal and will probably involve a few nested functions. At the moment I have the basic Ajax request. Needs to be POST, and doesn't have to handle multiple requests simultaneously.

var lastResponse = '';

function phpFunction(funcName){
var ajaxRequest; 

try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject('Microsoft.XMLHTTP');
        } catch (e){
            // Something went wrong
            alert('Ajax/Browser problem!');
            return false;
        }
    }
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){

                    //Seems to work.
        window.lastResponse = ajaxRequest.responseText;
    }
}
ajaxRequest.open('POST', 'ajaxCall.php', true);
ajaxRequest.send('func='+funcName); //Need to generate parameters dynamically, not sure how to do that
}

This shouldn't be really complex, but I've never seen a good implementation of it.

Any ideas?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug