405 POST请求错误(AngularJS $ http)

编程入门 行业动态 更新时间:2024-10-21 18:52:01
本文介绍了405 POST请求错误(AngularJS $ http)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想通过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)

本文发布于:2023-10-11 23:14:55,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   POST   AngularJS   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!