简体   繁体   English

AngularJS无法使用Content-Type:application / json发送post请求

[英]AngularJS can't send post request with Content-Type:application/json

Currently I am using angularJS and CoffeeScript to try to send a post request, and my sample code is: 目前我正在使用angularJS和CoffeeScript尝试发送帖子请求,我的示例代码是:

login: (user, callback)=>
baseUrl = 'http://localhost:3000/api/v1/sessions'
@$http({
  method: 'POST',
  url: baseUrl,
  data: user
}).success (result)->
  callback(result)

But when I call it, it just send 'OPTIONS' request instead of POST request. 但是当我调用它时,它只是发送'OPTIONS'请求而不是POST请求。
And the request detail is: 请求详细信息是: 在此输入图像描述 If I add header to this method, 如果我为此方法添加标头,

login: (user, callback)=>
baseUrl = 'http://localhost:3000/api/v1/sessions'
@$http({
  method: 'POST',
  url: baseUrl,
  data: user,
  headers:
    'Content-Type': 'application/json'
}).success (result)->
  callback(result)

It still doesn't works, but if I change headers to 'Content-Type': 'application/x-www-form-urlencoded' , then it can send post requests. 它仍然不起作用,但如果我将标题更改为'Content-Type':'application / x-www-form-urlencoded' ,那么它可以发送帖子请求。
But the request Content-type is not not I want. 但请求内容类型并不是我想要的。

I also try to modify the request data to JSON by: data: JSON.stringify(user) , but still not working. 我还尝试通过以下方法将请求数据修改为JSON: data:JSON.stringify(user) ,但仍然无效。

UPDATES 更新

Guys, I did another spike on this issue. 伙计们,我在这个问题上再次飙升。 Which is I am jquery to send the request and it works fine, but I found an wired thing that is they have different request data. 我发送请求是jquery,它工作正常,但我发现有线的事情是他们有不同的请求数据。

Jquery jQuery的

$.ajax({
  type: "POST",
  url: "http://localhost:3000/api/v1/sessions",
  data: {
    "user":{
      "email":"wahxxx@gmail.com",
      "password":"123456"
    }
  },
  success: function(){

  }

Screenshot for Jquery Jquery的屏幕截图 在此输入图像描述

Angular

login: (user, callback)=>
baseUrl = 'http://localhost:3000/api/v1/sessions'
@$http({
  method: 'POST',
  url: baseUrl,
  data: {
    "user":{
      "email":"wahxxx@gmail.com",
      "password":"123456"
    }
  },
  headers:
    'Content-Type': 'application/x-www-form-urlencoded'
}).success (result)->
  callback(result)

Now it can send request,but I just got 401 when trying to do request. 现在它可以发送请求,但我在尝试做请求时只有401。
Screenshot for Angular Angular的屏幕截图 在此输入图像描述

So I think the issue may due to the format of the angular request data. 所以我认为问题可能是由于角度请求数据的格式。

You are hitting CORS restrictions and same origin policy. 您正在达到CORS限制和相同的原始政策。

Easiest solution is to deploy web frontend and the api together as one app. 最简单的解决方案是将Web前端和api一起部署为一个应用程序。 If ports are different even on the same machine then one needs to deal with same origin policy. 如果即使在同一台机器上端口也不同,那么需要处理相同的源策略。

Options is a preflight query. 选项是预检查询。 Make your backend accept it and it should be fine. 让你的后端接受它,它应该没问题。

More reading: http://www.html5rocks.com/en/tutorials/cors/ 更多阅读: http//www.html5rocks.com/en/tutorials/cors/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery不会发送内容类型为application / json的发布请求 - jQuery doesn't send post request with content type application/json npm请求同时发送令牌和标头“ content-type”:“ application / json” - npm request send token and header 'content-type': 'application/json' at the same time 如何在$ resource AngularJS中将内容类型设置为DELETE方法的application / json - How can I set the content-type as application/json for DELETE method in $resource AngularJS 无法发布内容类型:application / json从角度到导轨 - Cannot POST with content-type: application/json from angular to rails Post Content-type undefined和application / json同时 - Post Content-Type undefined and application/json at the same time AJAX POST引发415不支持的媒体类型错误,并显示消息“请求必须具有“ Content-Type:application / vnd.api + json”” - AJAX POST throws a 415 Unsupported Media Type Error with message 'Request must have “Content-Type:application/vnd.api+json”' 使用POST设置请求标头的内容类型 - setting the request header content-type with POST 应该什么时候Content-Type是application / json - When should Content-Type be application/json 使用字符串作为请求正文时,为什么 Axios 发送我的 POST 请求时使用 Content-Type application/x-www-form-urlencoded? - Why does Axios send my POST request with Content-Type application/x-www-form-urlencoded when using a string as the request body? 如何设置基本身份验证、内容类型和数据以在 axios 发布请求中发送? - How to set basic auth, content-type and data to send in axios post request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM