簡體   English   中英

無法從php web服務上的http.post獲取數據

[英]unable to get data from http.post on php web service

我正在使用angular將數據發布到PHP JSON webservice但無法正確獲取它

這是我的.http方法的代碼

    app.controller('AddCustomer',['$http','$scope',function($http,$scope){
$scope.freshRequest = true;
$scope.addCustomer=function(){
    var data_to_send={};
    data_to_send.name=$scope.name;
    alert(data_to_send.name);
    $http({
        method: 'POST',
        url: '../service/add_new_Customer.php', 
        data: data_to_send,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function(returnedData){
        $scope.freshRequest = false;
        $scope.response = returnedData.response;
        $scope.accountNumber= returnedData.accountNumber;
        $scope.updatedBalance= returnedData.updatedBalance;
        $scope.transactionSuccessful = returnedData.transactionSuccessful;          
    }).error(function (data, status, headers, config) {
        $scope.status = status + ' ' + headers;
        console.log($scope.status)});



};}]);

下面是我用來調用addCustomer()方法的htm表單

<form class="form-horizontal" novalidate ng-controller="AddCustomer as add" ng-submit="addCustomer()">
<input id="name" name="name" type="text" placeholder="" class="form-control input-md" required="" ng-model="name">
<input id="fname" name="fname" type="text" placeholder="" class="form-control input-md" required="" ng-model="fname">
 <button id="button1id" type="submit" name="button1id" class="btn btn-primary">Add</button>

當我正在執行$ _REQUEST ['name']或$ _POST ['name']時,我的php為字段名稱變為null;

下面是php代碼

<?php
class Summary{
   public $response= "Account Could not be added invalid data";
   public $transactionSuccessful= "";
   public $updatedBalance= "";
   public $accountNumber= "";
}
$e = new Summary();
$final_res =json_encode($jsonObj) ;
if( $_REQUEST['name'] ){
$e->response =  $_REQUEST['name'];
}
else{
$e->response =  "Account Could not be added invalid data";
}
 $e->transactionSuccessful= true;
$e->updatedBalance=  $_POST['name'];
$e->accountNumber=  $_POST['name'];  
echo json_encode($e);

首先,您需要將發布的數據解碼為json,然后您可以訪問該對象的屬性。 file_get_contents("php://input")允許您讀取原始發布的數據。 這就是你如何獲得名字。

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$name = $request->name;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM