繁体   English   中英

Angularjs和PHP提交,POST和GET

[英]Angularjs & PHP submitting, POST and GET

我需要有关此代码的帮助。

当我单击此元素之一时

<li ng-repeat="mesa in mesas" ng-click="select($index,mesa.table_number)">
  <div class="round1" ng-if="selectedIndex === $index">&nbsp;</div>
  <div class="round" ng-if="selectedIndex !== $index">&nbsp;</div>
  MESA {{mesa.table_number}}</li>

angularjs的代码发布其值。

$scope.select = function (index, id){
    $scope.selectedIndex = index;
    $scope.options = true;
    $scope.idhttp = id;
    $http({
      method: 'POST',
      url: '../../php/getDetalles.php',
      data: {
        'mesa' : $scope.idhttp
      }
    }).then(function successCallback(data) {
        $scope.detalles = data
    }, function errorCallback(data) {
        alert('No Response');
    });

因此,然后使用元素编号的帖子,我试图获取此查询的结果。

PHP

<?php
include('base.php');
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$msfu = $request->mesa;
$data = array();
$result = mysql_query("SELECT name, price FROM orders WHERE table_number LIKE '$msfu'",$connect);
if(mysql_num_rows($result) > 0){
    while($row = mysql_fetch_assoc($result)){
        $data = $row;
    }
} else {
    echo "0 results";
};
echo json_encode($data);
mysql_close($connect);?>

则(更多ANGULARJS)

$http({
    method: 'GET',
    url: '../../php/getDetalles.php',
    }).then(function successCallback(data) {
        $scope.detalles = data,
        $scope.cuentas = data
    }, function errorCallback(data) {
        $scope.mesas = 'No Response';
    });

HTML

<div id="pedidos_table">
  <div id="pedidos_table_item" ng-repeat="detalle in detalles">
    <p id="pedidos_table_item_name">{{detalle.name}}</p>
    <p id="pedidos_table_item_price">{{detalle.price}}$</p>
  </div>
</div>

我正在尝试在$scope.idhttp上发布数字1 但是,即使在查询中我手动插入值1,代码也使我能够复制字段,而在字段上不打印任何内容(空字段)。 我为此疯了,有人可以帮助我吗?

$ http promise的.then方法使用response对象而不是data解析。

$scope.select = function (index, id){
    $scope.selectedIndex = index;
    $scope.options = true;
    $scope.idhttp = id;
    $http({
      method: 'POST',
      url: '../../php/getDetalles.php',
      data: {
        'mesa' : $scope.idhttp
      }
    //}).then(function successCallback(data) {
    }).then (function onSuccess(response)
        //DATA is property of response object
        var data = response.data;
        $scope.detalles = data
    }, function errorCallback(data) {
        alert('No Response');
    });

有关更多信息,请参见AngularJS $ http服务API参考-常规用法

解决后,我在$ data = $ row之后在php代码上添加了[]。 这样就结束了。 PHP

<?php
include('base.php');
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$msfu = $request->mesa;
$data = array();
$result = mysql_query("SELECT name, price FROM orders WHERE table_number = '$msfu'",$connect);
if(mysql_num_rows($result) > 0){
    while($row = mysql_fetch_assoc($result)){
        $data[] = $row;
    }
} else {
    echo "0 results";
};
echo json_encode($data);
mysql_close($connect);?>

ANGULARJS

$http({
      method: 'POST',
      url: '../../php/getDetalles.php',
      data: {
        'mesa' : $scope.idhttp
      }
    }).then (function onSuccess(response){
        var data = response.data;
        $scope.detalles = data
    }, function errorCallback(data) {
        alert('No Response');
    });

$http({
          method: 'GET',
          url: '../../php/getDetalles.php'
        }).then(function successCallback(response) {
            $scope.detalles = response.data,
            $scope.cuentas = response.data
        }, function errorCallback(response) {
            $scope.entradas = 'No Response';
        });

谢谢大家

@lin和@georgeawg。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM