我无法弄清楚为什么 GET 跨域请求有效,但使用完全相同的服务器 URL 的 POST 请求却没有。我在服务器上为所有请求方法(GET、POST、PUT、DELETE 和 OPTIONS)设置了以下响应标头(使用 JERSEY):
header("Access-Control-Allow-Origin", "*")
header("Access-Control-Allow-Credentials", "true")
header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
header("Access-Control-Allow-Headers", "accept, origin, authorization, content-type, content-length, connection, x-requested-with, user-agent")
GET 请求正在跨域工作
$.ajax({
type:"GET",
url: base_url + "workoutdays?memberId=100350194",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", auth);
},
success: function(msg)
{
$('#results').html(msg[0].workoutName);
},
error: function (xhRequest, errorText, thrownError)
{
alert(errorText);
}
});
但 POST 请求不是
$.ajax({
type:"POST",
url: base_url + "workoutdays?memberId=100350194",
data: {workoutId : "4"},
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", auth);
},
success: function(msg)
{
$('#results').html(msg[0].workoutName);
},
error: function (xhRequest, errorText, thrownError)
{
alert(errorText);
}
});