我试图做一个ajax' POST'在来自jquery的另一个域的laravel服务器上。我的laravel Route包含以下代码:
Route::post('auth', function(){
$data = Input::get('username');
return Response::json($data->toArray())->setCallback(Input::get('callback'),JSON_PRETTY_PRINT);
});
我的客户端来自不同的服务器,而JQuery ajax' POST'是:
function authUser(appId){
var data = '{"username": "' + appId + '"}';
$.ajax({
url: "http://localhost:8080/auth",
type: "POST",
dataType: 'jsonp',
data: JSON.stringify(data),
processData: false,
contentType: 'application/json',
CrossDomain:true,
async: false,
success: function (data) {
console.log(data);
},
error: function (xhr, ajaxOptions, thrownError) { //Add these parameters to display the required response
alert(xhr.status);
alert(xhr.responseText);
}
});
}
我在标题回复
中收到 405 Method Not Allowed这个问题的解决方案是什么?
答案 0 :(得分:2)
我通过将数据类型更改为 json 并在apache中添加标题来自行解决。
function authUser(appId){
var data = '{"username": "' + appId + '"}';
$.ajax({
url: "http://localhost:8080/auth",
type: "POST",
dataType: 'json',
data: JSON.stringify(data),
processData: false,
contentType: 'application/json',
CrossDomain:true,
async: false,
success: function (data) {
console.log(data);
},
error: function (xhr, ajaxOptions, thrownError) { //Add these parameters to display the required response
alert(xhr.status);
alert(xhr.responseText);
}
});
}
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, PUT, POST, DELETE, OPTIONS"
Header set Access-Control-Allow-Headers "Content-Type, Content-Range, Content-Disposition, Content-Description"
我知道访问控制允许来源的标题中的CORS到" *"使安全性失效,但如果有人需要解决方案,那就写在那里。