javascript - AngularJS and PHP Undefined property: stdClass -
i have simple post http request , getting error undefined property: stdclass::$number
, undefined property: stdclass::$message
. here 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);
there no data being passed , i'm getting undefined property error. missing here?
you setting undefined
inside sendmessage()
function over-write values user entered...so that's sent.
change declaration of model object outside function inherited 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
automatically add properties $scope.smsform
object why declared empty object here.
also since $scope.smsform
has properties need in api, there no need transfer them new object data
.
$http.post('app/endpoints/sendsms.php', $scope.smsform ).then(...
Comments
Post a Comment