【问题标题】:How do I use GET request for logging with Angularjs?如何使用 GET 请求使用 Angularjs 进行日志记录?
【发布时间】:2017-10-09 09:53:24
【问题描述】:

Angular JS:

// POST Data Works :
    $scope.submit = function(signinfo) {
    $http({
        url :'http://localhost:3000/loginfo',
        method : 'POST',
        data : signinfo
    })
    .then(
        function successCallback(result){
            response = {success: true, data: result}; 
        toastr.success('Successfully Updated');
        toastr.refreshTimer(toastr, 3000);

        $scope.reload();
    },
        function errorCallback(response){
            console.log("Error : " + response.data);
        });
    };


// GET Data Does Not Works : 
$http.get('http://localhost:3000/info')
.then(
    function successCallback(response){
        $scope.signinfo = response.data.info;
    },
    function errorCallback(response){
        console.log("Error : " + response.data)
    });

HTML:

<form ng-submit="loginform()" class="ng-scope ng-pristine ng-valid center" name="logform"><br/><br>
<tr ng-repeat="logcred in signinfo"></tr>

<div class="form-group col-md-4">
    <label form="emailinput"><b>Email</b></label>
<input type="email" class="form-control" name="uname" id="emailinput" placeholder="you@example.com" ng-model="username" >
</div>

<div class="form-group col-md-4">
<label form="pwdinput"><b>Password</b></label>
<input type="password" class="form-control" name="pwd" id="pwdinput" placeholder="*******" ng-model="password">
</div>

<a ng-click="reloadPage()" class="navbar-brand" ></a>

<div class="col-md-4">
    <button type="cancel" class="btn" ng-click="toggle_cancel()">Cancel</button>
    <button class="btn btn-primary">Login</button>
</div>
<br/>   
</form>

我已经使用 json 创建了登录页面表单,并尝试使用 GET 请求进行访问。我创建了 2 个表单一个用于注册,另一个用于登录。我使用POST 进行 signing 并且工作正常。对于 login,我使用了GET 请求,我认为这很简单,但它非常令人困惑。我不确定是否应该使用GETPOST 请求。我在为 login 页面编写函数时卡住了。

但是,我找不到一个例子。如果有人有任何可以分享的例子吗?

【问题讨论】:

    标签: javascript angularjs http


    【解决方案1】:

    您可以使用 Post 请求发送用户名和密码。在从 html 文件调用登录表单时传递用户名和密码。

    HTML 文件

    <form ng-submit="loginform(username,password)" class="ng-scope ng-pristine ng-valid center" name="logform"><br/><br>
    <tr ng-repeat="logcred in signinfo"></tr>
    
    <div class="form-group col-md-4">
        <label form="emailinput"><b>Email</b></label>
    <input type="email" class="form-control" name="uname" id="emailinput" placeholder="you@example.com" ng-model="username" >
    </div>
    
    <div class="form-group col-md-4">
    <label form="pwdinput"><b>Password</b></label>
    <input type="password" class="form-control" name="pwd" id="pwdinput" placeholder="*******" ng-model="password">
    </div>
    
    <a ng-click="reloadPage()" class="navbar-brand" ></a>
    
    <div class="col-md-4">
        <button type="cancel" class="btn" ng-click="toggle_cancel()">Cancel</button>
        <button class="btn btn-primary">Login</button>
    </div>
    <br/>   
    </form>
    

    JS 文件

    $scope.loginform = function(username,password){
        $http({
            url :'http://localhost:3000/loginfo',
            method : 'POST',
            data : {
                "userName":username,
                "password":password    
                }
        }).then(
        function successCallback(response){
            console.log(response)
        },
        function errorCallback(response){
            console.log("Error : " + response.data)
        });
    }
    

    【讨论】:

    • 我已经创建了POST,它工作正常。我实际上在看的是,当用户输入他的凭据时,用户名和密码应该与 json 匹配并登录
    • 您发布的代码将发布数据,但它不会与现有的json数据匹配
    • 好的。为此,您必须解析 json 数据,然后与输入的数据进行比较。
    • 其实我尝试解析数据,但我无法实现。如果您有任何工作示例,请分享它
    • 我已经创建了 plunker。我已经上传了那里的代码
    【解决方案2】:

    使用http get请求从json文件中获取数据。

    $http.get('db.json').success(function(response) {
                $scope.data= response;
            });
    

    【讨论】:

      猜你喜欢
      • 2017-09-30
      • 2014-11-17
      • 2016-06-05
      • 2014-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 2012-05-11
      相关资源
      最近更新 更多