Javascript Can';t在AngularJS中指定请求中的头
我的应用程序中有两个部分-Angular前端和rails服务器。由于它是不同的域,请求在默认情况下不起作用。这方面有很多员工,包括stack,但这对我不起作用 这是我在角度控制器中的方法:Javascript Can';t在AngularJS中指定请求中的头,javascript,ruby-on-rails,angularjs,cross-domain,angular-http,Javascript,Ruby On Rails,Angularjs,Cross Domain,Angular Http,我的应用程序中有两个部分-Angular前端和rails服务器。由于它是不同的域,请求在默认情况下不起作用。这方面有很多员工,包括stack,但这对我不起作用 这是我在角度控制器中的方法: $scope.test =-> # transform = (data) -> # $.param data $http.post('http://localhost:3000/session/create', 'token=some', headers: '
$scope.test =->
# transform = (data) ->
# $.param data
$http.post('http://localhost:3000/session/create', 'token=some',
headers:
'Access-Control-Allow-Origin': 'http://localhost:9000',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With'
# transformRequest: transform
).success (responseData) ->
console.log(responseData)
class SessionController < ApplicationController
respond_to :json, :html, :js
def create
respond_with "logged in"
# render nothing: true
end
end
我对转换数据进行了注释,在服务器响应中没有差异
我将应用程序(在一些帖子中看到)配置为:
.config ["$httpProvider", ($httpProvider) ->
$httpProvider.defaults.useXDomain = true
delete $httpProvider.defaults.headers.common["X-Requested-With"]
]
我想这使得请求不像ajax(但我不确定)
但我的请求中没有标题。它们都位于某些字段访问控制请求头中:访问控制允许来源、接受、访问控制允许头、访问控制允许方法、内容类型:
Request URL:http://localhost:3000/session/create
Request Method:OPTIONS
Status Code:404 Not Found
Request Headers
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,ru;q=0.6
Access-Control-Request-Headers:access-control-allow-origin, accept, access-control-allow-headers, access-control-allow-methods, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
DNT:1
Host:localhost:3000
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.8 Safari/537.36
Response Headers
Content-Length:12365
Content-Type:text/html; charset=utf-8
X-Request-Id:2cf4b37b-eb47-432a-ab30-b802b3e33218
X-Runtime:0.030128
Chrome控制台:
OPTIONS http://localhost:3000/session/create 404 (Not Found) angular.js:6730
OPTIONS http://localhost:3000/session/create No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. angular.js:6730
XMLHttpRequest cannot load http://localhost:3000/session/create. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. localhost/:1
其他无关紧要的信息:
在服务器上:
class ApplicationController < ActionController::Base
before_filter :allow_cross_domain_access
protected
def allow_cross_domain_access
headers['Access-Control-Allow-Origin'] = '*'# http://localhost:9000
headers['Access-Control-Allow-Headers'] = 'GET, POST, PUT, DELETE'
headers['Access-Control-Allow-Methods'] = %w{Origin Accept Content-Type X-Requested-With X-CSRF-Token}.join(',')
headers['Access-Control-Max-Age'] = '1728000'
end
end
我使用angular 1.2.0.rc-2的最新版本 您在示例中混淆了请求和响应头
在执行跨域请求(CORS)时,如果您希望执行与普通GET不同的任何操作(例如发布帖子或添加自定义标题),浏览器将首先发出选项请求。
这是您在开发人员控制台中看到的内容:
OPTIONS http://localhost:3000/session/create 404 (Not Found) angular.js:6730
然后,服务器应该在选项请求的响应中添加适当的头
因此,您可以从Angular调用中删除自定义标头。
接下来,您需要确保您的服务器/应用程序响应选项请求,而不是返回404。必须正确设置标题,您是否使用其他Web服务器将请求转发到rails应用程序服务器?我使用yeoman angular generator生成了应用程序。它基于grunt,可能有一些node.js服务器。你认为它能改变我的请求吗?第一个请求得到404响应。你能发布rake路由的输出吗?(或者在浏览器中转到)它不会响应选项,但这是飞行前请求所必需的。前端部分正在发出选项请求(正确),服务器正在响应404(很可能不正确)。这让我相信服务器就是问题所在。我这样做$http(方法:'OPTIONS',url:'http://localhost:3000/session/create).success(responseData)->console.log(responseData)
并添加到路由选项“会话/创建”
和console现在显示选项http://localhost:3000/session/create 500(内部服务器错误)
和下面相同的行。是的,你是对的,我删除了那些标题行,现在它可以工作了。我猜在rails端出现问题之前,由于其他原因无法正常工作。谢谢