简体   繁体   中英

Angularjs - Using $http on a HTTPS URL

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

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.

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 .

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

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.

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

Thanks for your help.

You might need to tell it to send the cookie:

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

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

Info on 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM