[英]jquery .ajax function not working properly with Laravel-4
我想做的是将变量从我的视图传递到控制器函数。问题是我从未被路由到控制器的函数存储。因此我无法检查是否也获得了变量。 ajax函数似乎可以正常工作(显示警报),但是我从未从控制器获得消息成功。
到目前为止,我的路线文件是:
Route::get('pois', array(
'uses' => 'MapController@create',
'as' => 'pois.create'
));
Route::post('pois', array(
'uses' => 'MapController@store',
'as' => 'pois.get'
));
我的MapController是:
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');
}
而且我的javascript-jquery脚本如下:
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);
}
}
贝娄也是我的HTML:
@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
您没有收到success
消息,因为您没有从控制器返回任何消息。
更改dd('success');
return 'success';
public function store()
{
return 'success';
}
您的Ajax代码:
$.ajax
({
type: "POST",
url: "pois",
data: {"id" : id},
success: function(response)
{
alert(response);
}
});
编辑:
将URL url: "pois"
更改为url: "/pois"
,
jQuery代码:
$( 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代码:
@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
我尚未测试代码,但应该可以。
在对控制器进行ajax调用时...您可以像这样检查
$jsonResponse = [
'imageReceived' => $r->hasFile('profilePicture')
];
return $jsonResponse;
这样将以true或false将响应返回到控制台,如下所示
{"imageReceived":false}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.