Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular.js:如何使用$http作为请求模块node.js_Node.js_Angularjs_Http_Request - Fatal编程技术网

Angular.js:如何使用$http作为请求模块node.js

Angular.js:如何使用$http作为请求模块node.js,node.js,angularjs,http,request,Node.js,Angularjs,Http,Request,在使用Angular.js的$http之前,我用node.js的请求模块测试了网站的反应。 我的目标是使用POST请求登录到一个网站,进行cookie会话,然后下载所有我想要的页面,请求的标题中包含此cookie 对于Node.js,它看起来是这样的: request.post({url: myUrl/login.php, form:{login:'login', password:'password'}}, function(err, resp, body){

在使用Angular.js的$http之前,我用node.js的请求模块测试了网站的反应。 我的目标是使用POST请求登录到一个网站,进行cookie会话,然后下载所有我想要的页面,请求的标题中包含此cookie

对于Node.js,它看起来是这样的:

request.post({url: myUrl/login.php, form:{login:'login', password:'password'}}, function(err, resp, body){
                var cookies = parseCookies(resp) //function that takes all the cookies 

                request({url: myUrl/portail, headers{cookie: cookies}}, function(err, resp, body){console.log('recu')})
            })
对post请求的响应实际上是重定向到myUrl/portail,标题中带有“set cookie”

我想使用Angular.js对我的应用程序执行此操作,因此我执行了以下操作:

$http({
    method: 'POST',
    url: myUrl/login.php,
    data: $.param({login: $scope.loginData.login, password: $scope.loginData.password}),
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data, status, headers) {
    var cookies = parseCookieAngularVersion(headers)
    $http({
      method:'GET',
      url: myUrl/portail,
      headers:{cookie:cookies}
    })
    .success(function(data, status, headers){
      console.log(headers())
    })
    .error(function(){
      console.log('ERRRROR')
    })
})
.error(function(data, status, headers, config) {
  console.log('ERRROR');
})
但它不起作用。。。标题不包含任何cookie,服务器会一直向我发送登录页面。问题一定是因为AngularJs是一个客户端应用程序。
有人能帮我吗?我会很好的。提前感谢

(1)确保您在
url
属性中有引号,即
url:“myUrl/login.php”,
而不是
url:myUrl/login.php,
。(2) 确保您没有违反任何同源策略,即提供上述代码的站点与您尝试从中获取的站点相同。所有url与Node.js代码中使用的url完全相同,这很好。现在,我成功地从post请求中获得了“set cookie”的cookie。“headers:{cookie:cookies}”(cookies类似于'id=1234309jdsq')是否正确?或者是否需要传递$cookieStore?