I made a post method to supply json data as api endpoint
My route is
Route::post('checkFuzzy', 'SolrController@checkFuzzy')->middleware('cors');
I made cors validate my origin but the ajax post method is not giving anything the ajax is as follows.
$('.searchme').on('click',function(){
var company = $("input[name=Company]").val();
console.log(company)
$.ajax({
headers: {
'Content-Type': 'text/html',
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
},
crossOrigin: true,
type: "post",
url: 'http://127.0.0.1:8006/api/checkFuzzy',
data: {
Company: function(){
return company
},
},
//data:'Company='+ company+'&Individual='+ individual,
success: function(msg){
alert('wow' + msg);
}
});
});
and the response in request headers is as follows
Provisional headers are shown
Accept: */*
Content-Type: text/html
Origin: http://localhost:8006
Referer: http://localhost:8006/searchSolr
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36
X-CSRF-TOKEN: e522xOu6IhXGCXLDyw929zCogVcXcaZ5yYWwXDo4
the Cors.php is as follows
public function handle($request, Closure $next)
{
$response = $next($request);
$response->headers->set('Access-Control-Allow-Origin' , '*');
$response->headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application, x-xsrf-token, x-csrf-token');
return $response;
}
The status code is 200.
The problem was with the return type. The return type should be a json not an array.
return response()->json([
'status' => 'success',
'result' => $calCulatedValue
]);