【问题标题】:jquery .ajax function not working properly with Laravel-4jquery .ajax 函数无法在 Laravel-4 中正常工作
【发布时间】:2014-01-14 07:29:04
【问题描述】:

我想要做的是将一个变量从我的视图传递给一个控制器函数。问题是我从来没有路由到我的控制器的函数存储区。所以我无法检查我是否也得到了变量。 ajax 功能似乎可以工作(显示警报),但我从来没有从我的控制器收到消息成功..

到目前为止,我的路线文件是:

Route::get('pois', array(
    'uses' => 'MapController@create',
    'as' => 'pois.create'
    ));

Route::post('pois', array(
    'uses' => 'MapController@store',
    'as' => 'pois.get'
    ));

我的地图控制器是:

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);

}

}

Bellow 也是我的 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


    【解决方案1】:

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

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

    这将像这样以 true 或 false 向控制台返回响应

    {"imageReceived":false}
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 不,它仍然不工作...... dd 函数应该工作......它是 var_dump 和 die 的快捷方式......问题是我永远无法达到我的商店功能......
      • 我认为,您的 Ajax URL 有问题。尝试将网址更改为/pois
      • 我也试过了...不过运气不好..介意我的脚本有一些 javascript 和一些其他 jquery 函数吗..?
      • 您使用的是哪个浏览器?如果您使用 Chrome,请检查开发人员工具中的网络部分。你会看到错误。
      • 您介意我更改您的 javascript 和 HTML。我将使用纯 JQuery 和 html5
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 1970-01-01
      • 2015-12-03
      相关资源
      最近更新 更多