2

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.

1
  • I am just coming across this issue. Did you ever resolve this? Commented Feb 11, 2016 at 10:54

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

It's not working. The $http call is still instantly rejected and I still have a pending OPTIONS request in the Network tab of the debugger. However, I managed to make it work by forcing the browser to bypass the OPTIONS request by setting a proper content-type and by disabling the X-Requested-With header : $httpProvider.defaults.headers.common['X-Requested-With'] = undefined; $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.