2014-02-13 29 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