2016-12-21 25 views
0

我一直在使用AngularJs一段時間,使用PHP/MySql作爲後端來獲取/發送信息到數據庫。在AngularJS中調用php函數或php文件AJAX

我(和它的工作)的方法是這樣的:

var myFunction = 'getUser'; 
$http.get('my/php/file.php?action='+myFunction).then(function(response) { 
    //get data here 
}) 

//or 

var myFunction = 'addUser'; 
$http.post('my/php/file.php?action='+myFunction, data).then(function(response) { 
    //get data here 
}) 

但是我也看到了很多的項目(教程,文章等),他們這樣稱呼它(或東西像這樣):

$http.get('api/user/:id').then(function(response) { 
    //get data here 
}) 

//or 
$http.post('api/user', data).then(function(response) { 
    //get data here 
}) 

正如你所看到的,我的方式,我宣佈我想用什麼功能,getUseraddUserdeleteUser,等等......但在我看到的例子,他們不」不這樣做,他們只是調用一個路徑引用。

一種方法或另一種方法的主要區別是什麼?何時(或爲什麼)我應該使用這個還是另一個?

回答

0

這一切都取決於服務,你發展

var myFunction = 'getUser'; 
$http.get('my/php/file.php?action='+myFunction).then(function(response) { 
    //get data here 
}) 

//or 

var myFunction = 'addUser'; 
$http.post('my/php/file.php?action='+myFunction, data).then(function(response) { 
    //get data here 
}) 

這一個對應於具有端點和請求和響應是通過他們處理的服務

而且此,

$http.get('api/user/:id').then(function(response) { 
    //get data here 
}) 

//or 
$http.post('api/user', data).then(function(response) { 
    //get data here 
}) 

對應於其中http動詞與相應資源匹配並且執行服務的RESTful服務

+0

但是這兩者之間的主要技術區別是什麼?因爲我可以看到兩者都可以返回相同類型(或預期)的數據信息。我對嗎? (我是新來的後端) – celsomtrindade