2015-09-06 46 views
0

我做了一個dropmenu,但是當我想發佈到特定頁面時有回帖。什麼都沒發生?我正在使用laravel框架。這是我的代碼:Dropdown postback

@extends('master') 
@section('title', 'Create a new ticket') 

@section('content') 

<script> 
$(document).ready(function() { 
    var xhr; 
    }); 
    $("#test").change(function(e) { 

    csrf = $("#token").attr('content') 
    option = $(this).val(); 

     $.ajax({ 
      url: '/receiveuserinformation', 
      type: 'POST', 
      data: { option_id: option }, 
      beforeSend: function(xhr){xhr.setRequestHeader('X-CSRF-TOKEN', csrf);}, 
      success: function(result) { 
       $("#kilometersprive").val(result); 
      } 
     }); 
    }); 
</script> 


<div class="form-group"> 
         <label for="content" class="col-lg-2 control-label">Standaard route</label> 
         <div class="col-lg-10"> 
           <select class="form-control input-sm" name="test" id="test"> 
           @foreach($standaardroute as $route) 
            <option value="{!! $route->id !!}">{!! $route->van !!} - {!! $route->naar !!}</option> 
           @endforeach 
           </select>    
         </div> 
        </div> 

在我的控制檯現在有錯誤嗎?

編輯

這是我的路線文件

Route::post('/receiveuserinformation','[email protected]'); 

這是我的路線@ createroute

public function createroute(Request $request) 
    { 
     $karakterrit = karakterrit::all(); 
     $foundroute = standaardroute::whereId($request->all())->firstorFail(); 
     $standaardroute = standaardroute::all(); 

     return view('ritten.create',compact('karakterrit',$karakterrit))->with('foundroute',$foundroute)->with('standaardroute',$standaardroute); 
    } 
+0

您需要添加更多信息。我們需要看你的routes.php和相應的控制器。添加從ajax調用返回的響應也會很有幫助。您可以在瀏覽器中的開發人員工具的網絡選項卡中看到此信息。 – user2479930

+0

請參閱我的編輯 – Jamie

回答

1

你肯定

url: '/receiveuserinformation', 

指向正確的網址是什麼?確保它使用URLs Helpers on Laravel Docs

也許你應該使用類似

url: {{ url("receiveuserinformation") }} 

就一定要指向總是到正確的網址。

+0

請參閱我的編輯! – Jamie

+0

你有一個名爲* route *的控制器嗎?這是我第一次看到這樣的事情。我建議你遵循命名約定的類和對象,以便編寫更好的代碼:)讓我知道*路由*控制器... –

+0

Oke,我的開發人員工具告訴我找不到請求的URL /接收用戶信息這臺服務器.......但這很奇怪,因爲它在我的路線文件? – Jamie

0

看起來你的代碼中有語法錯誤。您需要手動發佈到路線並查看您獲得的錯誤。或者,如果您使用Chrome等瀏覽器,則可以看到ajax調用正在使用開發人員工具返回的響應。

// Remove the optional id parameter as you don't need it if you are POSTing it. 
Route::post('/receiveuserinformation','[email protected]'); 

// Remove $id as you don't need it, and replace it with the request 
public function createroute(Request $request) 
{ 
    // Get the id from the POST data 
    $id = $request->input('option_id'); 

    $karakterrit = karakterrit::all(); 

    // You should really catch this exception if there isn't a matching id 
    $foundroute = standaardroute::whereId($id)->firstorFail(); 

    $standaardroute = standaardroute::all(); 

    return view('ritten.create', compact('karakterrit', 'foundroute', 'standaardroute')); 
}