javascript Angularjs - 在 HTTPS URL 上使用 $http

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17144460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:15:14  来源:igfitidea点击:

Angularjs - Using $http on a HTTPS URL

javascriptangularjs

提问by pdegand59

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

我正在尝试使用以下代码在 HTTPS URL 上使用 $http 服务:

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);
});

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.

my.custom.url 与我的 angularJS 应用程序位于不同的域中,但我的网络服务器配置良好,允许跨域 XHR 请求。它应该是一个公共网络服务。

When the request is sent, the promise is instantly rejected, so the error()function is triggered. The datais undefinedand the statusis 0.

当请求被发送时,promise 立即被拒绝,因此该error()函数被触发。该dataundefinedstatus0

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

在 Chrome 中调试器的网络选项卡上,我可以看到与我的$http.post()调用相对应的待处理 OPTIONS 请求。

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 $httpservice.

出于测试目的,我尝试使用 jQuery$.post()方法执行相同的请求,并且没有问题。我想我的$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.

请注意,这不是 XSRF 问题,如果我使用 Web 服务的 HTTP 版本,则请求成功。

Thanks for your help.

谢谢你的帮助。

回答by Mathew Berg

You might need to tell it to send the cookie:

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

In your config, DI $httpProviderand then set withCredentialsto true:

在您的配置中,DI$httpProvider然后设置withCredentialstrue

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

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

关于 angularjs withCredentials 的信息:http://docs.angularjs.org/api/ng .$http

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

哪些链接到 mozilla 文章:https: //developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale =en-US &redirectslug =HTTP_access_control#section_5