我想通过AngularJS $ http在Google Contacts API上发布帖子请求。
我试过这样做:
var config = {headers:{// Content-Type:application / json,Authorization: 持票人+ gapi.auth.getToken()。access_token,GData-Version:3.0}} var data = {test:test} $ http.post('www.google / m8 / feeds / contacts / default / full /',data,config);
但它返回 XMLHttpRequest无法加载 www.google/m8/feeds/contacts/default/full/ 。预检的响应包含无效的HTTP状态码405
此请求正在运行(返回201创建)在RestC上例如Postman。
如果有人能向我解释为什么这个方法不允许从浏览器中获得。
>解决方案我认为问题在于 www.google/m8/feeds/contacts API根本不会支持 OPTIONS 请求。 405 响应基本上是说端点不支持 OPTIONS 请求,不一定不支持 POST 。
解决方案是使用Google自己的 JavaScript API库,而不是Angular的 $ http 服务。
您可以使用JavaScript客户端库从您的Web应用程序与Google API(例如People,Calendar和Drive)进行交互。 / p>
还有关于Google的API库如何支持CORS请求的一些文档。
这个库可能有一些Angular包装版本你的生活有点容易,但我还没有挖到。
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('www.google/m8/feeds/contacts/default/full/', data, config);
But it returns "XMLHttpRequest cannot load www.google/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.
解决方案I believe the issue is that the www.google/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.
更多推荐
405 POST请求错误(AngularJS $ http)
发布评论