[英]Jquery: Cross-domain ajax 'POST' with laravel
我正在嘗試從jquery的另一個域在laravel服務器上執行ajax'POST'。我的laravel路由包含以下代碼:
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
這個問題有什么解決方案?
我自己解決了這一問題,方法是將數據類型更改為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"
我知道Access-Control-Allow-Origin標頭中的CORS為“ *”會使安全性失效,但是如果有人需要解決方案,它將被寫在那里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.