php - 如何使用php和angularJS获取数据

标签 php mysql angularjs

希望您心情愉快:)。我正在尝试返回依赖于使用 AngularJS 和 php 的用户文本的数据。所以我创建了包含我的查询的文件 php,并在 AngularJS 中使用 $http.get。我的问题是我想在 mysql 查询中集成输入值,但总是使用 Angular 。我尝试了很多次但没有任何效果为了更清楚,这里有很多代码:

应用程序.js

app1.controller('autoCompleteCTRL',function($scope,$rootScope,$http) {
    $scope.search = function(){
        $scope.$watch('searchText', function() {
            console.log($scope.searchText);
        });
        $http({
            method: "get",
            url: "../SearchResultsQuery.php",
            dataType: 'json',
            data: { 'userText': $scope.searchText},
            async: false,
            params: {action: "get"}
        }).success(function(data, status, headers, config) {
            alert(data);
        }).error(function(data, status, headers, config) {           
            alert(error);               
        });
    };
}

index.html

<input type="text" placeholder="Search for items" id="textFiled" class="input"  ng-model="searchText" ng-change="search()" />

应用.php

<?php

$conn=pgsqlCon();
$searchText = mysql_real_escape_string(userText);
$query='SELECT * FROM planet_osm_roads AS a, to_tsquery_partial(\'%.$searchText.%\') AS query
WHERE ts_road @@ query';
$result = pg_query($conn,$query);
$myarray = array();
while ($row = pg_fetch_row($result)) {
    $myarray[] = $row;
}
pg_close($conn);
echo json_encode($myarray);

?>

我希望你能理解我,谢谢你提前:)

最佳答案

试着像这样发出你的 http 请求:

app.controller('TestCtrl', function ($scope, $http){

    $scope.search = function(){
        $http.post('../app.php', {searchText: $scope.searchText})
        .success(function (response){
            console.log(response);
        })
        .error(function (response){
            console.log(response);
        });
    }

});

应用.php

<?php
    $data = file_get_contents("php://input");
    $searchText = mysql_real_escape_string($data['searchText']);
    /* database code */
?>

这应该有效。

关于php - 如何使用php和angularJS获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36773829/

相关文章:

javascript - 为什么当我单击按钮时会加载我们的页面

php - Nginx 特殊文件夹到另一个目录

mysql - 使用 GROUP BY 获取单个值 - MySQL

javascript - Angular : update scope inside scope

php - 使用 phpseclib nlist 从后缀/扩展名过滤的 SFTP 文件夹中获取文件

php - A* 搜索 - 最少跳数?

php - 如何从 php MySQL 中的两个不同表更新表?

mysql - 从 wp_post 表中删除 ID

javascript - 是否可以在不触发关联的 $route 的情况下更改 $location

javascript - AngularJS http 获取错误的 json 文件