如何在cakephp中向RESTAPI发出ajax请求

如何在cakephp中向RESTAPI发出ajax请求,ajax,rest,cakephp,mobile,Ajax,Rest,Cakephp,Mobile,我在cakephp中有一个用户组件,它有add/update/delete controllers方法,充当restapi方法 现在我想通过ajax请求或http请求调用这些控制器 此rest api将使用移动应用程序 谢谢你的帮助 将此代码添加到您的页面: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></scri

我在cakephp中有一个用户组件,它有add/update/delete controllers方法,充当restapi方法

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

此rest api将使用移动应用程序


谢谢你的帮助

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

<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>

jQuery.ajax({
类型:'POST',//Le-type-de-ma-requete
url:'ajaxresponse.php',//将其发送到php文件
数据:{
string:'aStringHere'//发送此JSON变量中的字符串
}, 
成功:函数(数据、文本状态、jqXHR){
//在变量中,数据将存储在您将要生成的php文件中的ajax函数的响应中
//你想在这里干什么
警报(数据);
},
错误:函数(jqXHR、textStatus、errorshown){
//如果可能发生错误,请执行其他操作
}
});
这应该是您的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";
   }
 ?>