我想从jQuery向AJAX POST请求添加自定义标头.
我试过这个:
$.ajax({
type: 'POST',
url: url,
headers: {
"My-First-Header":"first value",
"My-Second-Header":"second value"
}
//OR
//beforeSend: function(xhr) {
// xhr.setRequestHeader("My-First-Header", "first value");
// xhr.setRequestHeader("My-Second-Header", "second value");
//}
}).done(function(data) {
alert(data);
});
Run Code Online (Sandbox Code Playgroud)
当我发送此请求并使用FireBug观看时,我看到此标题:
选项xxxx/yyyy HTTP/1.1
主机:127.0.0.1:6666
User-Agent:Mozilla/5.0(Windows NT 6.1; WOW64; rv:11.0)Gecko/20100101 Firefox/11.0
接受:text/html,application/xhtml + xml, application/xml; q = 0.9,/ ; q = 0.8
Accept-Language:fr,fr-fr; q = 0.8,en-us; q = 0.5,en; q = 0.3
Accept-Encoding:gzip,deflate
Connection:keep -alive
Origin:null
Access-Control-Request-Method:POST
Access-Control-Request-Headers:my-first-header,my-second-header
Pragma:no-cache
Cache-Control:no-cache
为什么我的自定义标题会转到Access-Control-Request-Headers:
Access-Control-Request-Headers:my-first-header,my-second-header
我期待像这样的标头值:
My-First-Header:第一个值
My-Second-Header:第二个值
可能吗?
我试图在另一个应用程序(spring-boot应用程序)上调用REST端点(angularjs).应用程序在以下主机和端口上运行.
http://localhost:8080http://localhost:50029我也在使用spring-securityspring-boot应用程序.从HTML应用程序,我可以对REST应用程序进行身份验证,但此后,我仍然无法访问任何REST端点.例如,我有一个如下定义的angularjs服务.
adminServices.factory('AdminService', ['$resource', '$http', 'conf', function($resource, $http, conf) {
var s = {};
s.isAdminLoggedIn = function(data) {
return $http({
method: 'GET',
url: 'http://localhost:8080/api/admin/isloggedin',
withCredentials: true,
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
});
};
s.login = function(username, password) {
var u = 'username=' + encodeURI(username);
var p = 'password=' + encodeURI(password);
var r = 'remember_me=1';
var data = u + '&' + p + '&' + r;
return $http({
method: 'POST',
url: 'http://localhost:8080/login',
data: …Run Code Online (Sandbox Code Playgroud) ajax ×1
angularjs ×1
cors ×1
http-headers ×1
javascript ×1
jquery ×1
post ×1
rest ×1
spring-boot ×1
spring-mvc ×1