Reputation: 43
I'm trying to make a call to a laravel controller with this route:
Route::post('obtenerNodos', 'panelController@obtenerNodos');
but I just get : 500 , error, Internal Server Error
$.ajax({
url: "obtenerNodos",
data: { id: '1' },
method: 'post',
success: function () {
console.log("done");
//do something
},error: function(xhr, ajaxOptions, thrownError){
console.log(xhr.status+" ,"+" "+ajaxOptions+", "+thrownError);
}
});
this is the controller:
namespace App\Http\Controllers;
use GuzzleHttp\Client; use Illuminate\Http\Request;
class panelController extends Controller {
public function viewpanel(){
return view('admin.panel');
}
public function viewstats(){
return view('admin.estadisticas');
}
public function viewfacs(){
$client = new Client();
$res = $client->get('http://smartbill.co:332/SmartBill2/rest/factura/get?codigo=FA001100x01&token=fd25834e78c7c4f806e9e56b307d16c18a847197');
$respuesta = json_decode( $res->getBody()->getContents(),true);
return view('admin.factura_add')->with('facturas', $respuesta);
}
public function viewfacsreg(){
return view('admin.factura_reg');
}
public function cerrarSesion(){
//matar sesion antes de hacer la redirreccion
return view('welcome');
}
public function obtenerNodos($id){
$id = Input::get("id");
return "done";
}
}
And this is the full error:
POST http://binario.com/obtenerNodos 500 (Internal Server Error) send @ jquery.min.js:6 ajax @ jquery.min.js:6 (anonymous) @ viewfacsreg:199
Upvotes: 1
Views: 517
Reputation: 2945
keep your route like this :
Route::post('obtenerNodos', 'panelController@obtenerNodos');
change your obtenerNodos()
function to this :
public function obtenerNodos(Request $request){
$id = $request->id;
return "done";
}
and don't forget to import this at the top : use Illuminate\Http\Request;
Add this JavaScript that sets defaults for all ajax request in jQuery. Preferably in a js file that is included across your app:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
})
make sure you have a meta tag like this in your blade page :
<meta name="csrf-token" content="{{ csrf_token() }}">
Upvotes: 1