405 Error with POST request (AngularJS $http)

2019-07-14 06:16发布

I want to do a post request on Google Contacts API with AngularJS $http.

I tried to do this :

var config = {
    headers: {
        // "Content-Type": "application/json",
        "Authorization": "Bearer " + gapi.auth.getToken().access_token,
        "GData-Version": "3.0"
    }
}

var data = {
    "test": "test"
}

$http.post('https://www.google.com/m8/feeds/contacts/default/full/', data, config);

But it returns "XMLHttpRequest cannot load https://www.google.com/m8/feeds/contacts/default/full/. Response for preflight has invalid HTTP status code 405"

This request is working (returns 201 created) on a RestClient like Postman.

If someone could explain to me why this method is not allowed from a browser.

1条回答
Fickle 薄情
2楼-- · 2019-07-14 06:21

I believe the issue is that the https://www.google.com/m8/feeds/contacts API simply doesn't support OPTIONS requests. The 405 response is essentially saying the endpoint doesn't support the OPTIONS request, not necessarily that it doesn't support POST.

The solution is to use Google's own JavaScript API library rather than Angular's $http service.

You can use the JavaScript client library to interact with Google APIs, such as People, Calendar, and Drive, from your web applications.

There's also some documentation on how Google's API library supports CORS requests.

There may be some Angular wrapped versions of this library to make your life a little easier however I haven't dug around.

查看更多
登录 后发表回答