[英]AJAX send post request REST API (Provisional headers are shown)
I'm trying to send post request data
to my api
. 我正在尝试将
post request data
发送到我的api
。
The api
works fine in my postman, 该
api
在我的邮递员中工作正常,
But in my laravel project, I'm getting the caution
like this 但是在我的laravel项目中,我得到了这样的
caution
Provisional headers are shown 显示临时标题
API to Send Post Request Data 发送帖子请求数据的API
url: https://api.mydomain.com/api/login
{ "companyID":"2018-101", "password":"123456" }
Content-Type : application/json
Accept : application/json
In my Laravel project here's my ajax
that performs the sending of post
data to the api
. 在我的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);
}
});
Here's my Cors.php
这是我的
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');
}
}
My Kernel.php 我的Kernel.php
\\App\\Http\\Middleware\\Cors::class,
UPDATED 更新
Tried to change the 试图改变
data:{ companyID:"2018-101", password:"123456" },
to 至
data: JSON.stringify({ "companyID": "2018-101", "password" : "123456" }),
contentType: "application/json",
Remove the headers:{'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
删除
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);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.