2017-02-28 31 views
1

我知道這是重複的問題,但我面臨的問題與苗條框架webservices的angularjs。使用angularjs(例如404)發佈瘦身框架?

我創建Web服務來獲得學生網址的記錄是這樣的:

http://www.slim.local/api/getstudent/1 

代碼苗條(網絡服務):

//Get Single user 
$app->get('/api/getstudent/{id}',function(Request $request, Response $response){ 
    $id = $request->getAttribute('id'); 

    $sql = "select * from student where id = $id"; 
    try{ 
     //Get object db 
     $db = new db(); 
     //connect 
     $db = $db->connect(); 
     $stmt = $db->query($sql); 
     $student = $stmt->fetchAll(PDO::FETCH_OBJ); 
     $db = null; 
     echo json_encode($student); 
    }catch(PDOException $e){ 
     echo '{"Error":{"text":'.$e->getMessage().'}'; 
    } 

}); 

上面的代碼正在與郵差測試。

當我們試圖打相同的URL從angularjs其給出404錯誤和網址,這樣創建:GET http://www.slim.local/api/getstudent?id=2 404 Not Found

角代碼:

RoutingApp = angular.module('RoutingApp', ['ngRoute']); 

RoutingApp.config([ '$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) 
{ 
    $routeProvider 
     .when('/home', { 
      templateUrl: 'template/home.html', 
      controller: 'homeController', 
      controllerAs: "homeCtrl" 
     }) 
     .when('/courses', { 
      templateUrl: 'template/courses.html', 
      controller: 'coursesController', 
      controllerAs: "coursesCtrl" 
     }) 
     .when('/students', { 
      templateUrl: 'template/students.html', 
      controller: 'studentsController', 
      controllerAs: "studentsCtrl" 
     }).when('/students/:id', { 
     templateUrl: 'template/studentDetails.html', 
     controller: 'studentDetailsController', 
     controllerAs: "studentDetailsCtrl" 
    }) 
     .otherwise({ 
      redirectTo: '/home', 
     }); 
    $locationProvider.html5Mode(true); 
} ]).controller('studentDetailsController', function($http,$routeParams){ 
    var vm = this; 
    $http({ 
     url: "http://www.slim.local/api/getstudent", 
     params: {id:$routeParams.id}, 
     method: "get" 
    }).then(function(response){ 
     vm.student = response.data; 
    }); 
}); 

我們怎樣才能搭配超薄API angularjs網址與查詢字符串的網址?

+0

同樣的問題,我也面臨。 –

回答

0

你做了錯誤的方式

試試這個

$http({ 
    url: "http://www.slim.local/api/getstudent", 
    params: {id:$routeParams.id}, 
    method: "get" 
}).then(function(response){ 

$http({ 
    url: "http://www.slim.local/api/getstudent/" + $routeParams.id, 
    method: "get" 
}).then(function(response){ 

它給404錯誤,因爲在路線已設置的路由參數

http://www.slim.local/api/getstudent/{id} //here id is route parameter 

和你逝去的GET參數

http://www.slim.local/api/getstudent?id=2 //here id is $_GET parameter 
4

只需改變你所創建的HTTP GET請求的方式:

$http({ 
     url: "http://www.slim.local/api/getstudent/"+$routeParams.id, 
     method: "get" 
    }) 

這是因爲你的苗條API提供的網址http://www.slim.local/api/getstudent/1而不是http://www.slim.local/api/getstudent?id=1