what I am trying to do is to pass a variable from my view to a controller function..The problem is that I am never routed to my controller's function store.So I cant check if I get the variable as well. The ajax function seems to work(the alert is displayed) but I never get the message success from my controller..
So far my route file is :
Route::get('pois', array(
'uses' => 'MapController@create',
'as' => 'pois.create'
));
Route::post('pois', array(
'uses' => 'MapController@store',
'as' => 'pois.get'
));
My MapController is:
public function create()
{
$bar=new Map;
$rest=new Map;
$mag=new Map;
$bar = Map::where('type', '=', 'bar')->take(10)->get();
$rest = Map::where('type', '=', 'restaurant')->take(10)->get();
$mag = Map::where('type', '=', 'magazine')->take(10)->get();
return View::make('pois.markers')->with('bar',$bar)->with('rest',$rest)->with('mag',$mag);
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
dd('suceess');
}
And my javascript-jquery script is bellow:
function ajaxCall(id) {
$.ajax
({
type: "POST",
url: "pois",
data: {"id" : id},
success: function(response)
{
alert('ok');
}
});
}
function checkBoxes(elem){
if(document.getElementById(elem).checked==true){
ajaxCall(elem);
}
else{
clearMarkers(elem);
}
}
Bellow is my HTML as well :
@section('main_contents')
<br/>
<div class="text-center">
<label class="checkbox-inline">
<input type="checkbox" id="bar" name="group" value="option1" onClick="checkBoxes(this.id)"> Καφετέριες
</label>
<label class="checkbox-inline">
<input type="checkbox" id="restaurant" name="group" value="option2" onClick="checkBoxes(this.id)"> Εστιατόρια
</label>
<label class="checkbox-inline">
<input type="checkbox" id="magazine" name="group" value="option3" onClick="checkBoxes(this.id)"> Μαγαζιά
</label>
<br/>
</div>
<div id="map" style="height:500px;"></div>
@stop
You are not receiving success
message because you are not returning any message from your controller.
change dd('success');
to return 'success';
public function store()
{
return 'success';
}
Your Ajax code:
$.ajax
({
type: "POST",
url: "pois",
data: {"id" : id},
success: function(response)
{
alert(response);
}
});
Edit:
Change URL url: "pois"
, to url: "/pois"
,
Jquery code:
$( document ).ready(function() {
$(".checkbox").change(function() {
if(this.checked) {
var id = $('.checkbox').val();
var request = $.ajax({
url: '/pois',
type: "POST",
data: { id : id },
dataType: "html"
});
request.done(function(result) {
if(result == 'success') {
alert("Success");
} else {
alert ("Sorry! unable to add the data");
}
});
request.fail(function(jqXHR, textStatus) {
console.log( "Request failed: " + textStatus );
});
}
});
});
HTML code:
@section('main_contents')
<br/>
<div class="text-center">
<label class="checkbox-inline">
<input type="checkbox" id="bar" class="checkbox" name="group" value="1"> Καφετέριες
</label>
<label class="checkbox-inline">
<input type="checkbox" id="restaurant" class="checkbox" name="group" value="2"> Εστιατόρια
</label>
<label class="checkbox-inline">
<input type="checkbox" id="magazine" class="checkbox" name="group" value="3"> Μαγαζιά
</label>
<br/>
</div>
<div id="map" style="height:500px;"></div>
@stop
I haven't tested the code but should work.
While making ajax call to controller... you can check like this
$jsonResponse = [
'imageReceived' => $r->hasFile('profilePicture')
];
return $jsonResponse;
This will return response to console with true or false like this
{"imageReceived":false}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.