簡體   English   中英

Angular $ http 404錯誤

[英]Angular $http 404 error

嘗試發送發帖請求時收到404錯誤,我不確定為什么。 同時收到“可能未處理的拒絕”錯誤。 我是Angular的新手,所以任何提示都將不勝感激。 我查看了文檔,發現了有關$ http.post請求結構的各種其他信息,但是到目前為止,我還不能將其應用於我在這里嘗試做的事情:

ng從我的html文件中單擊:

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

我的JS文件中的方法調用

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'

聽起來像是注入問題,您可以確保已將所有依賴項文件注入到應用程序/控制器嗎?

我認為這是我使用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');
});

當我應該在路由器帖子中僅顯示“ /”時。 當然,“ /電影/電影”路線不存在。

暫無
暫無

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

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