0

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

2
  • Where are you calling saveEditPage function? Could you please add that part of the template to the question as well? Commented Dec 8, 2013 at 13:21
  • saveEditPage is called by the template on form submit. The function is called correctly but the post itself is not firing. I've added the template code. Commented Dec 8, 2013 at 13:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, can't believe such a small typo caused such troubles. But now error is getting fired rather than success. Could this be a post headers problem?
Well, this could be anything, eg. your url could be wrong. Just open the development console and see the HTTP result yourself - Crtl+Shift+I in Chrome/FF and open the Network tab.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.