【发布时间】:2015-02-22 07:14:17
【问题描述】:
当我使用 php 函数 substr($id,1) 转义参数冒号 (:id) 时,我的苗条路线 (index.php) 仅将数据返回到 angular (app.js)。
当我注释 substr 函数时,返回为 false,因为参数 id 之前的冒号。
为什么会这样?
app.js(角度)
var app = angular.module('app',['ngRoute']);
app.config(['$routeProvider',function($routeProvider) {
$routeProvider.
when('/',{controller: 'ListCtrl',templateUrl:'partials/list.html'}).
when('/product/:param',{controller:'ListIdCtrl',templateUrl:'partials/list.html'}).
otherwise({redirectTo:'/'});
}]);
app.controller('ListIdCtrl',['$scope','$http','$routeParams', function($scope,$http,$routeParams){
var requestId = $routeParams.param;
console.log(requestId);
$http.get("../product/" + requestId).success(function(data) {
console.log(data); //return false
}).
error(function(){
console.log('Error on get json');
});
}]);
index.php (slim)
require 'lib/Slim/Slim.php'; //include the framework in the project
\Slim\Slim::registerAutoloader(); //register the autoloader
$app = new \Slim\Slim(array());
$app->get('/product/:id','getProduct');
$app->run();
function getProduct($id) {
$sql = "select * from wines where id =:id";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$id = substr($id, 1); //need to use substr to remove colon on :id
$stmt->bindParam(":id", $id, PDO::PARAM_INT);
$stmt->execute();
$wine = $stmt->fetchObject();
$db = null;
echo json_encode($wine);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function getConnection() {
return $dbh;
}
【问题讨论】: