Angularjs - 对HTTPS URL使用HTTP $ [英] Angularjs - Using $http on a HTTPS URL

查看:839
本文介绍了Angularjs - 对HTTPS URL使用HTTP $的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用下面的code对HTTPS URL使用$ http服务:

I am trying to use the $http service on a HTTPS URL with the following code :

var request = $http.post('https://my.custom.url/webservice', privateAttributes.requestData);
request.success(function(data, status) {

}).error(function(data, status) {
    console.log('data -', data);
    console.log('status -', status);
});

该my.custom.url是不同的域作为我的angularJS应用程序,但我的web服务器很好配置为允许跨域XHR请求。它应该是一个公共Web服务。

The my.custom.url is on a different domain as my angularJS app, but my webserver is well configured to allow cross domain XHR request. It's supposed to be a public webservice.

在发送请求,承诺会立即拒绝,因此错误()功能被触发。在数据未定义状态 0

When the request is sent, the promise is instantly rejected, so the error() function is triggered. The data is undefined and the status is 0.

在我的Chrome浏览器的调试器的网络选项卡上,我可以看到对应我的 $ http.post()呼叫挂起OPTIONS请求。

On the network tab of my debugger in Chrome, I can see a pending OPTIONS request corresponding to my $http.post() call.

有关测试目的,我试图做的jQuery $同样的要求。后()方法,它没有问题的工作。我想我做什么毛病 $ HTTP 服务。

For testing purpose, I tried to do the same request with jQuery $.post() method and it worked with no issue. I think I am making something wrong with the $http service.

请注意,这不是一个XSRF的问题,如果我用我的web服务的HTTP版本,该请求是成功的。

Please note that it's not a XSRF issue and if I use the HTTP version of my webservice, the request is a success.

感谢您的帮助。

推荐答案

您可能需要告诉它发送Cookie:

You might need to tell it to send the cookie:

在你的配置,DI $ httpProvider ,然后将 withCredentials 真实

In your config, DI $httpProvider and then set withCredentials to true:

.config(function ($routeProvider, $httpProvider) {
    $httpProvider.defaults.withCredentials = true;
    //rest of route code

在angularjs withCredentials信息: http://docs.angularjs.org/api/ng 。$ HTTP

Info on angularjs withCredentials: http://docs.angularjs.org/api/ng.$http

可链接到Mozilla的文章:<一href=\"https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control#section_5\" rel=\"nofollow\">https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control#section_5

Which links to the mozilla article: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control#section_5

这篇关于Angularjs - 对HTTPS URL使用HTTP $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆