2014-02-13 28 views
0

我在cakephp中有一个用户组件,它具有用作REST API方法的添加/更新/删除控制器方法。如何让ajax请求在cakephp中休息api

现在我想从ajax请求或http请求调用这些控制器。

这个休息API将消耗移动应用程序。

感谢您的帮助!

回答

0

将此代码添加到您的网页:

<script type="text/javascript" 
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js">  
</script> 
<script>   
jQuery.ajax({ 
    type: 'POST', // Le type de ma requete 
    url: 'ajaxresponse.php', // Send it to a php file 
    data: { 
    string: 'aStringHere' // send a string in this JSON variable 
    }, 
    success: function(data, textStatus, jqXHR) { 

    // In the variable data will be stored your response from your ajax function in the php file you will make 
    //Do whaever you want with it here 
      alert(data); 
        }, 
        error: function(jqXHR, textStatus, errorThrown) { 
        // If an error can happens, do something else 
        } 
       }); 
</script> 

这里,这应该是你的PHP文件:

<?php 
    // Here you check if you received a string 
    if($_POST['string'] == true) 
    { 
    // Do something here, like calling cakephp controllers and this should do it 
     $avariable = "hello world!"; 
     echo "Whaever you put here will be sent back to your ajax function, like this variable $avariable"; 
    } 
?> 
+0

如果你给我任何的CakePHP API方法调用这将是伟大的.. – Pravin