2017-04-10 148 views
0

我試圖發送一個post請求時收到404錯誤,我不知道爲什麼。還得到'可能未處理的拒絕'錯誤。我對Angular很陌生,所以任何提示將不勝感激。我看過的文件過來,發現周圍$ http.post請求的結構的各種其他信息,但到目前爲止,我不能把它應用到我想要在這裏做:

NG-點擊從我的HTML文件:在我的JS文件

<button ng-click="addToFavorites(searchResults.response.data)" class="btn btn-success" type="button" name="addToFavorites">Add to Favorites</button> 

方法調用

addToFavorites: function(movie) { 
      var newMovie = isNewMovie(movie); // verify if movie has already been favorited 
      if (newMovie) { // add to favoriteMovies if it's a new movie 
      console.log(movie); 
      // favoriteMovies.push(movie); 
      $http.post('/m', movie).then(function(response) { 
       console.log(response.data); 
      }); 
      // .then(function(response) { 
      // console.log(response); 
      // }).catch(function(err) { 
      // console.log('error:', err); 
      // }); 
      } else { // alert user if it's already been favorited 
      alert('This movie is already in your list of favorites.'); 
      } 
     } // end addToFavorites() 

全部JS文件:

var pmdbApp = angular.module('pmdbApp', []); 

pmdbApp.controller('InputController', ['$scope', 'MovieService', function($scope, MovieService) { 
    console.log('InputController loaded'); 
    $scope.title = ''; // data-bound to user input field 
    // $scope.searchForm = MovieService.searchForm; 
    $scope.searchOMDB = MovieService.searchOMDB; // data-bound to user button click 
    // reference to searchResults object 
    // object contains the OMDB response as a property 
    $scope.searchResults = MovieService.searchResults; 
    $scope.getPoster = MovieService.getPoster; // bound to 'Search OMDB' button 
    $scope.addToFavorites = MovieService.addToFavorites; // bound to 'Add to Favorites' button 
}]); // end 'InputController' 

pmdbApp.controller('OutputController', ['$scope', 'MovieService', function($scope, MovieService) { 
    console.log('OutputController loaded'); 
    $scope.movieService = MovieService; 
}]); // end 'OutputController' 

pmdbApp.factory('MovieService', ['$http', function($http) { 
    // searchResults object will be used to store response from the OMDB API 
    var searchResults = {}; 
    /*var searchForm = { 
    title: '', 
    searchResults: {} 
    };*/ 
    var favoriteMovies = []; 

    function isNewMovie(movie) { 
    for (var i = 0; i < favoriteMovies.length; i++) { 
     if (movie.imdbID === favoriteMovies[i].imdbID) { 
     return false; 
     } 
    } 
    return true; 
    } 

    // var saveToDatabase = function(movie) { 
    // console.log('got here with movie', movie); 
    // $http.post('/movies', movie).then(function(response) { 
    //  console.log('response'); 
    // }); 
    // } 

    // public information 
    return { 
    favoriteMovies: favoriteMovies, 
    searchResults: searchResults, // pass an object referece 
    // searchForm: searchForm, 
    searchOMDB: function(title) { 
     $http.get('http://www.omdbapi.com/?t=' + title).then(function(response) { 
     console.log(response); 
     if (response.data.Error) { // alert user if no movie matches search results 
      alert('Movie not found!'); 
     } else { // otherwise store response as an object property 
      searchResults.response = response; 
     } 
     }); // end $http.get 
    }, // end searchOMDB() 
    addToFavorites: function(movie) { 
     var newMovie = isNewMovie(movie); // verify if movie has already been favorited 
     if (newMovie) { // add to favoriteMovies if it's a new movie 
     console.log(movie); 
     // favoriteMovies.push(movie); 
     $http.post('/m', movie).then(function(response) { 
      console.log(response.data); 
     }); 
     // .then(function(response) { 
     // console.log(response); 
     // }).catch(function(err) { 
     // console.log('error:', err); 
     // }); 
     } else { // alert user if it's already been favorited 
     alert('This movie is already in your list of favorites.'); 
     } 
    } // end addToFavorites() 
    }; // end return 
}]); // end 'MovieService' 
+0

每當我收到404時,都會使用Chrome的網絡調試選項卡清除。如果你有服務器的日誌,你的服務器日誌也會有幫助。 – kontiki

+1

404表示您請求的網址不存在。 – Sorikairo

+0

如上所述,該URL不存在或者您嘗試的URL不正確。通過在任何瀏覽器或任何http客戶端工具中點擊完整的URL來檢查URL是否可用。它會幫助你確定問題 – CrazyMac

回答

0

我認爲這是我如何使用Angular的錯誤,但實際上它只是路由設置中的錯誤。

客戶端後:

$http.post('/movies', movie).then(function(response) { 
     console.log(response.data); 
    }); 

在我的主服務器上的文件:

app.use('/movies', movies); 

在我的路由器我有:

router.post('/movies', function(req, res) { 
    console.log('/movies route hit with movie:', req.body); 
    res.send('work, please'); 
}); 

當我應該只是 '/' 在路由器後。當然'/電影/電影'路線不存在。

0

這聽起來像是一個注入問題,你能確保你已經注入所有的依賴文件到你的應用程序/控制器嗎?