簡體   English   中英

Angular $ http.post未到達服務器

[英]Angular $http.post not reaching the server

我在啟動$ http.post時遇到問題:

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");
            });
    };

});

模板代碼:

<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>

不幸的是,$ http.post不會觸發。 我試着將郵寄電話包裹在$ scope。$ apply周圍,但也沒有用。

我怎樣才能解決這個問題?

謝謝

編輯:固定

JavaScript變量名稱區分大小寫。 您已經聲明了postOBject但是正在傳遞postObject

ReferenceError:未定義postObject

如果我糾正了錯字,它可以按我的預期工作。

順便說一句,我建議將IDE與靜態分析一起使用-它會立即通知您未定義的變量。 此外,Firebug或Chrome DevTools javascript控制台對於javascript開發幾乎是絕對必要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM