drhe80011 2015-02-06 14:07
浏览 53

为什么我需要使用substr php函数来转义:Slim index.php route参数中的冒号

My slim route (index.php) only return data to angular (app.js) when i escape parameter colon (:id) using php function substr($id,1).

When i comment the substr function the return is false because the colon before parameter id.

Why this?

app.js (angular)

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;
}
  • 写回答

1条回答

  • duandongji2231 2015-02-21 23:14
    关注

    I found the problem on html href:

    before

    <a class="text-link" href="#/product/:{{vinho.id}}" ><h4>{{vinho.username}}</h4></a>
    

    after i use ng-href withou colon

    <a class="text-link" ng-href="#/product/{{vinho.id}}" ><h4>{{vinho.username}}</h4></a>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀