2016-08-16 60 views
0

我正在嘗試執行登錄表單。我正在使用angularjs的ng-submit。當點擊提交按鈕時,我想要在服務器中調用post方法而不更改url。發送表單請求以表示不更改網址

express的post需要它響應的url。如果我的登錄屏幕的網址是http://localhost:2000/login,並且點擊提交時,我希望post被調用相同的網址http://localhost:2000/login

而且我不明白如何使用$httpurl密鑰。

登錄HTML:

<form ng-controller="loginController" ng-submit="loginFunc()"> 
<input type="text" name="" ng-model="login"> 
<input type="password" ng-model="password" name=""> 
<input type="submit" value="login" name=""> 

控制器:

.controller('loginController', function($scope, $http) { 
    $scope.login = "" 
    $scope.password = ""; 
    $scope.loginArray = []; 
    $scope.loginFunc() { 
    $scope.loginArray.push($scope.login); 
    $scope.loginArray.push($scope.password); 
    $http({ 
     method: 'POST', 
     url: 'loginClick', 
     data: $scope.loginArray 
    }) 
    } 
}) 

的server.js:

app.get('/login', function(request, response) { //the file sent when /login is requested 
    response.sendFile(__dirname + "/staticFolder/view/login.html"); 
}) 

app.post('/loginCheck', function(request, response) { //call this when submit button is clicked without change in url 
    console.log(JSON.stringify(request.body) + "req"); 
}) 
+0

任何錯誤,你越來越?? – vkstack

回答

0

嘗試發送的登錄信息爲對象,而不是發送在array.It會讓你的生活更輕鬆。

.controller('loginController', function($scope, $http) { 
    $scope.loginFunc() { 
     $http({ 
       method: 'POST', 
       url: '/loginCheck', //not loginClick make sure you have a route to handle this. 
       data: { 
        login: $scope.login, 
        password: $scope.password 
       } 
      }) 
      .then(function(response) { 
       //Success handling 
      }, function(err) { 
       //Error handling 
      }); 
    } 
}) 

你必須在每一個路線迴應

編輯1個 使用後端爲:使用相同的URL /login,但不同的HTTP動詞(getpost

app.get('/login', function(request, response) { //the file sent when /login is requested 
    response.sendFile(__dirname + "/staticFolder/view/login.html"); 
}); 
app.post('/login', function(request, response) { //call this when submit button is clicked without change in url 
    console.log(JSON.stringify(request.body) + "req"); 
    /* 
    below will be the logic for handling your interest. 
    */ 
    someIOCall(function Handler(err,resultJson){ 
    if(err) 
     return res.status(500).end(); 
    res.status(200).end(resultJson) 
    })  
}) 

它應該在場景中工作。

+0

我不認爲有人瞭解我的問題。當我想顯示login.html時,URL是'../ login.html'。當我點擊登錄按鈕時,我想調用'post'方法。 'post'方法需要一個url。我不想指定那個url,因爲我想留在同一個url中,post方法應該回應相同的 – Nobody

+0

1.要呈現登錄表單,url將是'/ login'而不是'/ login.html'和2。如果你做一個Ajax請求,你必須處理數據,無論你得到什麼... 並請分享確切的問題你想達到什麼沒有指定。 – vkstack

0

你應該發送POST數據爲對象,不作爲數組,如果不表示d oes不知道哪個參數具有哪個值。

另外,像'loginClick'這樣的字符串不能用作url。

嘗試類似:

$scope.loginFunc = function(){ 
     $http({ 
      method:'POST', 
      url:'/login', 
      data: { 
       login: $scope.login, 
       password: $scope.password 
      } 
     }) 
    } 

不是100%肯定是「/ login」是不夠的URL,如果不嘗試用端口指定絕對URL等

如果你想發佈到

「/登錄」,你必須調整你的路線明確,以及:

app.post('/login', function(request,response) { 
    console.log(JSON.stringify(request.body)+"req"); 
}) 
+0

當我第一次顯示登錄屏幕時,然後請求被髮送到服務器,並在'app.post'中搜索請求參數。我想這樣做只有當登錄按鈕被點擊檢查登錄。 – Nobody

+0

感謝您關於作爲對象發送而不是陣列的建議。 – Nobody