Heim >Backend-Entwicklung >PHP-Tutorial >php模块化供前端ajax调用的实现 ajax php post jquery ajax php ajax分

php模块化供前端ajax调用的实现 ajax php post jquery ajax php ajax分

WBOY
WBOYOriginal
2016-07-29 08:50:291274Durchsuche

背景:没有使用php框架
由于临时需求,需要ajax调用php中的方法,简单的写了一个php文件,文件定义了两个方法,如何用ajax分别调用同一个php文件不同的方法。
以下是abc.php文件,我定义的两个方法a方法和b方法

<code><span><span><?php </span><span><span>class</span><span>abc</span>
    {</span><span><span>function</span><span>a</span><span>()</span>{</span><span>echo</span> something;
        }
        <span><span>function</span><span>b</span><span>(<span>$args</span>)</span>{</span><span>echo</span> something;
            }
        }
    }
<span>?></span></span></span></code>

以下是Controller.php,这个文件是调用其他具体的功能类的控制器,起到枢纽作用,主要是通过反射来实现

<code><span><span><?php </span><span>if</span> (!<span>empty</span>(<span>$_REQUEST</span>[<span>'action'</span>])) {  
        <span>try</span> {    
            <span>$action</span> = explode(<span>'/'</span>, <span>$_REQUEST</span>[<span>'action'</span>]);    
            <span>$class_name</span> = <span>$action</span>[<span>0</span>];    
            <span>$method_name</span> = <span>$action</span>[<span>1</span>];    
            <span>require</span><span>$class_name</span> . <span>'.php'</span>;    
            <span>$class</span> = <span>new</span> ReflectionClass(<span>$class_name</span>);    
            <span>if</span> (class_exists(<span>$class_name</span>)) {      
                <span>if</span> (<span>$class</span>->hasMethod(<span>$method_name</span>)) {        
                    <span>$func</span> = <span>$class</span>->getmethod(<span>$method_name</span>);        
                    <span>$instance</span> = <span>$class</span>->newInstance();        
                    <span>$func</span>->invokeArgs(<span>$instance</span>, <span>array</span>(<span>$_REQUEST</span>));                              
                }    
            }  
        } 
        <span>catch</span> (<span>Exception</span><span>$exc</span>) {    
            <span>echo</span><span>$exc</span>->getTraceAsString();  
        }
    }
<span>?></span></span></span></code>

使用ajax调用时,使用如下写法实现只调用abc.php中的a方法:

<code>$.ajax({
         type:<span>"POST"</span>,
         url: <span>'Controller.php?action=abc/a'</span>,    
         dataType: <span>'json'</span>,
         success: <span><span>function</span><span>(data)</span> {</span>         }
      });</code>

如果需要传参数,可以这么写来调用:

<code>$.ajax({
         <span><span>type</span>:"<span>POST</span>",</span>
         url: '<span>Controller</span>.php',   
         <span><span>data</span>:<span>{
            <span>action</span>=<span>abc</span>/<span>b</span>,
            参数1: 123,
            参数2: 456}</span></span>
         dataType: 'json',
         success: function(<span><span>data</span>) <span>{

         }</span></span>
      });</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了php模块化供前端ajax调用的实现,包括了ajax,php方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn