我正在尝试将post request data
发送到我的api
。
api
在我的邮递员中工作正常,
但是在我的laravel项目中,我得到了这样的caution
显示了临时标题
用于发送发布请求数据的API
url: https://api.mydomain.com/api/login
{ "companyID":"2018-101", "password":"123456" }
Content-Type : application/json
Accept : application/json
在我的Laravel项目中,这是我的ajax
,用于将post
数据发送到api
。
$.ajax({
headers:{'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
url: "https://api.mydomain.com/api/login",
method: "POST",
data:{
companyID:"2018-101",
password:"123456"
},
dataType: "json",
success:function(data)
{
alert("Successfully Login!");
},
error: function(xhr, ajaxOptions, thrownError){
console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
这是我的Cors.php
<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
/**
* Handle an incoming request.
*a
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next) {
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
->header('Access-Control-Allow-Headers',' Origin, Content-Type, Accept, Authorization, X-Request-With')
->header('Access-Control-Allow-Credentials',' true');
}
}
我的Kernel.php
\App\Http\Middleware\Cors::class,
已更新
试图更改
data:{
companyID:"2018-101",
password:"123456"
},
到
data: JSON.stringify({ "companyID": "2018-101", "password" : "123456" }),
contentType: "application/json",
答案 0 :(得分:1)
删除headers:{'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
$.ajax({
url: "https://api.mydomain.com/api/login",
method: "POST",
data:{ companyID:"2018-101", password:"123456" },
dataType: "json",
success:function(data)
{
alert(data.data.access_token);
},
error: function(xhr, ajaxOptions, thrownError){
console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});