jquery .ajax函数与Laravel-4无法正常工作

问题描述 投票:0回答:2

我想要做的是将一个变量从我的视图传递给一个控制器函数。问题是我从来没有路由到我的控制器的函数库。所以我不能检查我是否也得到了变量。 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
javascript php jquery ajax laravel-4
2个回答
1
投票

您没有收到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

我没有测试过代码但应该可以工作。


0
投票

在对控制器进行ajax调用时......你可以像这样检查

$jsonResponse = [
                 'imageReceived' =>  $r->hasFile('profilePicture')
                ];
return $jsonResponse;

这将返回对控制台的响应,如下所示为true或false

{"imageReceived":false}
© www.soinside.com 2019 - 2024. All rights reserved.