使用AJAX从Laravel中的数据库检索数据

使用AJAX从Laravel中的数据库检索数据,ajax,dynamic,drop-down-menu,laravel,Ajax,Dynamic,Drop Down Menu,Laravel,好吧,我是拉威尔的新手。如果有人能帮我解决这个问题,它可以为我解释很多。我想做的事情是将这个PHP ajax请求转换为laravel ajax。这是一个动态选择框 PHP和ajax代码是: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.

好吧,我是拉威尔的新手。如果有人能帮我解决这个问题,它可以为我解释很多。我想做的事情是将这个PHP ajax请求转换为laravel ajax。这是一个动态选择框

PHP和ajax代码是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Sections Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".country").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;

$.ajax
({
type: "POST",
url: "ajax_city.php",
data: dataString,
cache: false,
success: function(html)
{
$(".city").html(html);
} 
});

});
});
</script>
<style>
label
{
font-weight:bold;
padding:10px;
}
</style>
</head>

<body>
<div style="margin:80px">
<label>Country :</label> <select name="country" class="country">
<option selected="selected">--Select Country--</option>
<?php
include('db.php');
$sql=mysql_query("select id,data from data where weight='1'");
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$data=$row['data'];
echo '<option value="'.$id.'">'.$data.'</option>';
 } ?>
</select> <br/><br/>
<label>City :</label> <select name="city" class="city">
<option selected="selected">--Select City--</option>

</select>



</div>
</body>
</html>
public function city()
    {
        $city= City::all();
        return View::make('country', ['city'=>$city]);
    }
我的路线

Route::post('/contry/city', 'RegistrationController@city');
我的阿贾克斯之旅

$(".country").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;

$.ajax
({
type: "POST",
url: "/country/city",
data: dataString,
cache: false,
dataType : "html",
success: function(html)
{
$(".stage").html(html);
} ,
// code to run if the request fails; the raw request and
    // status codes are passed to the function
    error: function( xhr, status, errorThrown ) {
        alert( "Sorry, there was a problem!" );
        console.log( "Error: " + errorThrown );
        console.log( "Status: " + status );
        console.dir( xhr );
    },

    // code to run regardless of success or failure
    complete: function( xhr, status ) {
        alert( "The request is complete!" );
    }
});

});

那是,,我不知道,但是,我的意见是,如果你使用的是laravel,为什么要使用mysql\u query?我没有使用mysql query,我正在尝试将mysql查询转换为laravel thingsIs
Route::post('/contry/city','RegistrationController@city');输入错误?您键入了“contry”。您还可以检查从
app/storage/logs/laravel.log
中得到的错误消息吗RegistrationController@city'); 这是一个打字错误。我得到了错误500。如果在您看来,您将如何使用MVC将上面的php代码转换为laravel?
$(".country").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;

$.ajax
({
type: "POST",
url: "/country/city",
data: dataString,
cache: false,
dataType : "html",
success: function(html)
{
$(".stage").html(html);
} ,
// code to run if the request fails; the raw request and
    // status codes are passed to the function
    error: function( xhr, status, errorThrown ) {
        alert( "Sorry, there was a problem!" );
        console.log( "Error: " + errorThrown );
        console.log( "Status: " + status );
        console.dir( xhr );
    },

    // code to run regardless of success or failure
    complete: function( xhr, status ) {
        alert( "The request is complete!" );
    }
});

});