douran7929 2015-09-30 13:26 采纳率: 100%
浏览 65
已采纳

AngularJS和PHP Undefined属性:stdClass

I have a simple POST http request and is getting an error Undefined property: stdClass::$number and Undefined property: stdClass::$message. Here are my codes:

smsController.js

angular
    .module('smsApp')
    .controller('smsController', smsController)
    .config(config);

function config($routeProvider) {
    $routeProvider
    .when('/', {
        controller: 'smsController',
        templateUrl: 'app/views/messageForm.html'
    });
}

function smsController($scope, $http) {

    $scope.sendMessage = function() {
        $scope.loading = true;
        $scope.smsForm = {
            number: undefined,
            message: undefined
        };

        var data = {
            number: $scope.smsForm.number,
            message: $scope.smsForm.message
        };

        var req = {
            method: 'POST',
            url: 'app/endpoints/sendsms.php',
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            data: data
        };

        $http(req)
        .then(function successCallback(response) {
            console.log(response);
            $scope.loading = false;
          }, function errorCallback(response) {
            console.log(response);
          });
    }

}

messageForm.html

<div ng-hide="loading">
  <div class="input-field col s12">
    <input type="text" ng-model="smsForm.number" maxlength="11">
  </div>
  <div class="input-field col s12">
    <textarea ng-model="smsForm.message" maxlength="200"></textarea>
    <button ng-click="sendMessage()">Send</button>
  </div>
</div>

sendsms.php

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
echo json_encode($request);

Console enter image description here

There is no data being passed and I'm getting the undefined property error. Am I missing something here?

  • 写回答

1条回答 默认 最新

  • doubeizhong5178 2015-09-30 13:39
    关注

    You are setting undefined inside the sendMessage() function which will over-write any values user entered...so that's what will get sent.

    Change declaration of the model object to be outside the function so that it will get inherited by child scopes:

    FROM

    $scope.sendMessage = function() {
        $scope.loading = true;
        $scope.smsForm = {
            number: undefined,
            message: undefined
        };
        ....
        // ajax code 
     }
    

    TO

     $scope.smsForm = {};
    
     $scope.sendMessage = function() {
        $scope.loading = true;
    
        ....
        // ajax code
     }
    

    ng-model will automatically add the properties to the $scope.smsForm object which is why it is declared as an empty object here.

    Also since $scope.smsForm has the properties that you need in api, there is no need to transfer them to a new object data.

    $http.post('app/endpoints/sendsms.php', $scope.smsForm ).then(...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目