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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里