angular.js - 关于angularjs post的问题
伊谢尔伦
伊谢尔伦 2017-05-15 17:07:35
0
2
499
<!DOCTYPE html>
<html lang="en-us" ng-app="myApp">
    <head>
        <title>Document</title>
        <meta http-equiv="X-UA-Compatible" content="IE=Edge">
        <meta charset="UTF-8">

        <!-- load bootstrap and fontawesome via CDN -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <style>
            html, body, input, select, textarea
            {
                font-size: 1.05em;
            }
        </style>
        
        <!-- load angular via CDN -->
       <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
        <script src="app.js"></script>
    </head>
    <body>

        <header>
            <nav class="navbar navbar-default">
            <p class="container">
                <p class="navbar-header">
                    <a class="navbar-brand" href="/">AngularJS</a>
                </p>

                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#"><i class="fa fa-home"></i> Home</a></li>
                </ul>
            </p>
            </nav>
        </header>

        <p class="container">

            <p ng-controller="mainController">
            
                <p>
                    <label>What is your twitter handle?</label>
                    <input type="text" ng-model="handle" />
                </p>
                
                <p class="alert" ng-class="{ 'alert-warning': handle.length < characters, 'alert-danger': handle.length > characters }" ng-if="handle.length !== characters">
                
                    <p ng-show="handle.length < characters">
                        You have less than 5 characters!
                    </p>
                    <p ng-show="handle.length > characters">
                        You have more than 5 characters!
                    </p>
                    
                </p>
                
                <hr />
                
                <h1>twitter.com/{{ lowercasehandle() }}</h1>
                
                <h3>Rules</h3>
                <ul>
                    <li ng-repeat="rule in rules">
                        {{ rule.RuleName }}
                    </li>
                </ul>

                Add rule: <input type="text" ng-model="newRule" /><a href="#" class="btn btn-default" ng-click="addRule()">Add</a>
                
            </p>
            
        </p>

    </body>
</html>
//app.js
var myApp = angular.module('myApp', []);

myApp.controller('mainController', ['$scope', '$filter','$http',function ($scope, $filter,$http) {

    $scope.handle = '';

    $scope.characters = 5;

    $scope.lowercasehandle = function () {
        return $filter('lowercase')($scope.handle);
    }

    $http.get('/angularjs/http/api.json')

         .success(function(result){
            $scope.rules = result;
         })

         .error(function (data,status) {
             
             console.log(data);
         })

    $scope.newRule = '';

       $scope.addRule = function () {
        $http.post('/angularjs/http/api', { newRule: $scope.newRule })
            .success(function (result) {

                console.log(result);
                $scope.rules = result;
                $scope.newRule = '';

            })
            .error(function (data, status) {

                console.log(data);

            });
    };
}]);
//api.json
[
    {
    "ID":1,
    "RuleName":"Must be 5 characters"
     },{
    "ID":2,
    "RuleName":"Must be 5 characters"
     },{
    "ID":3,
    "RuleName":"Must be 5 characters"
     }
]

能get到数据,但是post的时候没有更新json数据,和Accept:application/json, text/plain, */* 这个有关吗?该怎么解决。。

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
Peter_Zhu

When getting, you can get the data by using any local server or directly reading the json file without a server. But if you want to save the changed content on the page, you need server-side code to handle it. Angular's own $http does not have the function of writing files. 想保存数据的话,post的接口需要有对应的代码逻辑进行文件操作才行.

洪涛

Post the code for processing json data on the backend, cool!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template