本文介绍了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('https://www.google .com / m8 / feeds / contacts / default / full /',data,config);

但它返回 XMLHttpRequest无法加载。预检的响应包含无效的HTTP状态码405



此请求正在运行(返回201创建)在RestC上例如Postman。



如果有人能向我解释为什么这个方法不允许从浏览器中获得。

>解决方案

我认为问题在于 https://www.google.com/m8/feeds/contacts API根本不会支持 OPTIONS 请求。 405 响应基本上是说端点不支持 OPTIONS 请求,不一定不支持 POST



解决方案是使用Google自己的,而不是Angular的 $ http 服务。

还有关于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('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.

解决方案

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.

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:04
查看更多