SrgHartman
SrgHartman

Reputation: 651

Angular $http.post not reaching the server

I'm having a problem getting $http.post to fire:

app.controller('editPageController', function($scope, $routeParams, $http) {

    $scope.page = $routeParams.pageid;

    // get page data from server
    $http.get('/pages/' + $scope.page).
        success(function(data, status, headers, config) {
            $scope.Name = data[0].name;
            $scope.Content = data[0].content;
            $scope.Location = data[0].location;
        }).
        error(function(data, status, headers, config) {
            alert("Can't get the page from the server");
        });

    // save page data on the server
    $scope.saveEditPage = function() {

        var postOBject = {Name: $scope.Name, Content: $scope.Content, Location: $scope.Location};
        $http.post('/pages/' + $scope.page + '/edit', postObject).
            success(function(data, status, headers, config) {
                alert("success");
            }).
            error(function(data, status, headers, config) {
                alert("Can't edit the page on the server");
            });
    };

});

The template code:

<script type="text/ng-template" id="editPage.html">
    <h1>Edit page:</h1>
    <form ng-submit="saveEditPage()">
    <p>Name:</p>
    <input type="text" ng-model="Name" value="{{Name}}">
    <p>Content:</p>
    <textarea ng-model="Content">{{Content}}</textarea>
    <p>Location:</p>
    <input type="text" ng-model="Location" value="{{Location}}">
    <p><input type="submit" value="Save"> <input type="button" value="Cancel" ng-click="$back()"></p>
</form>

Unfortunately the $http.post does not fire. I tried wrapping the post call around $scope.$apply and it didn't work either.

How can I fix this?

Thanks

EDIT: FIXED

Upvotes: 0

Views: 652

Answers (1)

Ondrej Machulda
Ondrej Machulda

Reputation: 1010

JavaScript variable names are case sensitive. You have declared postOBject but you are passing postObject.

ReferenceError: postObject is not defined

If I correct the typo, it's working as expected for me.

BTW I recommend using IDE with static analysis - it will inform you about undefined variables immediately. Also Firebug or Chrome DevTools javascript console are almost absolutely necessary for javascript development.

Upvotes: 1

Related Questions