$ http POST没有返回正确的响应

时间:2017-08-04 19:28:26

标签: javascript angularjs post http-post angularjs-http

我遇到的问题是$ http POST没有返回正确的响应。 我需要的数据存储在 config 下,而不是数据

Http POST请求:

for (var i = 0; i < filmService.filmData.length; i++) {
      filmData.push({
        title     :    filmService.filmData[i].title,
        overview  :    filmService.filmData[i].info,
        poster    :    filmService.filmData[i].poster,
        genres    :    filmService.filmData[i].genres,
        release   :    filmService.filmData[i].release

      }); 
}
  var data = angular.toJson(filmData[0]);


     $http({
            method: 'POST',
            url:'/search',
            data: data,
            headers: {
              'Content-Type': 'application/json;charset=utf-8'
          },
        }).then(function successCallback(response) {
            console.log(response); //response received
    });

我进入控制台的响应 - 以RED突出显示的是我需要的数据:

The part highlighted in RED is the data I need but its in CONFIG.

2 个答案:

答案 0 :(得分:1)

这样做,你应该得到你想要的东西:

  $http({
        method: 'POST',
        url:'/search',
        data: data,
        headers: {
          'Content-Type': 'application/json;charset=utf-8'
      },
    }).then(function successCallback(response) {
        var data = response.data
        console.log(data); //response received
});

答案 1 :(得分:-1)

.then函数内返回config.data。

结果将是这样的

$http({
        method: 'POST',
        url:'/search',
        data: data,
        headers: {
          'Content-Type': 'application/json;charset=utf-8'
      },
    }).then(function successCallback(response) {
        var data = response.config.data
        console.log(data); //response received
});