美元AngularJS http。post server不允许带有方法选项的请求

时间:2022-08-22 20:18:10

I am trying to do a an HTTP POST to a server.

我正在尝试对服务器执行一个HTTP POST。

The data I have to send is a json object.

我必须发送的数据是一个json对象。

The problem is that $http.post in angular override the method with options.

问题是$http。在角度覆盖方法与选项。

I can make this config

我可以设置这个配置

.config(['$httpProvider', function ($httpProvider) {
  //Reset headers to avoid OPTIONS request (aka preflight)
  $httpProvider.defaults.headers.common = {};
  $httpProvider.defaults.headers.post = {};
  $httpProvider.defaults.headers.put = {};
  $httpProvider.defaults.headers.patch = {};
}])

and changes from options to POST, but I can't set the content-type to "application/json", and I am getting a "415 Unsupported Media Type"

还有从选项到POST的更改,但是我不能将内容类型设置为“application/json”,我得到的是“415不支持的媒体类型”

Thank you

谢谢你!

2 个解决方案

#1


6  

$http.post in angular doesn't override the method with OPTIONS. It appear that you are trying to call api in different domain than the one your JS code come from. This is called Cross Domain. For such cases the browser performs preflight request with OPTIONS in order to see the returned headers. In your backend response you should add the header Access-Control-Allow-Origin: * for example. When the browser sees that header he performs the actual POST request.

美元http。在角度上的post不会用选项覆盖方法。看起来,您正在尝试调用不同领域的api,而不是您的JS代码。这叫做交叉域。对于这种情况,浏览器执行带有选项的飞行前请求,以查看返回的头部。例如,在后端响应中,应该添加头访问控制允许的起源:*。当浏览器看到该标题时,他执行实际的POST请求。

More details here: https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS

更多细节在这里:https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS

Hope this is helps!

希望这是帮助!

#2


1  

Add

添加

$httpProvider.defaults.headers.post['Content-Type'] = 'application/json';

But note this will set the Content-Type header globally.

但是请注意,这将在全局设置Content-Type头。

If you need to set the content-type per call, you should use $http.post like

如果需要设置每次调用的内容类型,应该使用$http。后像

 $http.post("/foo/bar", requestData, {
        headers: { 'Content-Type': 'application/json'},
        transformRequest: transform
    }).success(function(responseData) {
        //do stuff with response
    });

#1


6  

$http.post in angular doesn't override the method with OPTIONS. It appear that you are trying to call api in different domain than the one your JS code come from. This is called Cross Domain. For such cases the browser performs preflight request with OPTIONS in order to see the returned headers. In your backend response you should add the header Access-Control-Allow-Origin: * for example. When the browser sees that header he performs the actual POST request.

美元http。在角度上的post不会用选项覆盖方法。看起来,您正在尝试调用不同领域的api,而不是您的JS代码。这叫做交叉域。对于这种情况,浏览器执行带有选项的飞行前请求,以查看返回的头部。例如,在后端响应中,应该添加头访问控制允许的起源:*。当浏览器看到该标题时,他执行实际的POST请求。

More details here: https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS

更多细节在这里:https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS

Hope this is helps!

希望这是帮助!

#2


1  

Add

添加

$httpProvider.defaults.headers.post['Content-Type'] = 'application/json';

But note this will set the Content-Type header globally.

但是请注意,这将在全局设置Content-Type头。

If you need to set the content-type per call, you should use $http.post like

如果需要设置每次调用的内容类型,应该使用$http。后像

 $http.post("/foo/bar", requestData, {
        headers: { 'Content-Type': 'application/json'},
        transformRequest: transform
    }).success(function(responseData) {
        //do stuff with response
    });