php 使用 Laravel 控制器从 $.ajax 调用接收数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22850384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Receive data from an $.ajax call with laravel controller
提问by Yunus Einsteinium
I am trying to take selected option from <select>
, pass it through to method show()
in controller, use it to retrieve data from database, and then return it as json to the success method in $.ajax
. All this is happening through jquery.ajax
.
我正在尝试从 中获取所选选项<select>
,将其传递给show()
控制器中的方法,使用它从数据库中检索数据,然后将其作为 json 返回到$.ajax
. 这一切都是通过jquery.ajax
.
My Problem
我的问题
How can i get/receive data i pass from $.ajax
request to manipulate the database?
我如何获取/接收我从$.ajax
请求中传递的数据以操作数据库?
Here is the code I am using:
这是我正在使用的代码:
routes.php
路由文件
Route::post('rate/units',array('as'=>'unitRoute','uses'=>'RateController@show'));
RateController.php
速率控制器.php
public function show()
{
$row[] = $_POST['deg'];
return json_encode($row);
}
Views
观看次数
<select class="form-control choosedegree" name="sem" id="semester">
<option value="" selected="selected" disabled>Select Semester</option>
<option value="1">Year 1, Semester 1</option>
<option value="2">Year 1, Semester 2</option>
<option value="3">Year 2, Semester 1</option>
<option value="4">Year 2, Semester 2</option>
<option value="5">Year 3, Semester 1</option>
<option value="6">Year 3, Semester 2</option>
<option value="7">Year 4, Semester 1</option>
<option value="8">Year 4, Semester 2</option>
</select>
<script>
$(document).ready(function() {
$('select#semester').on('change', function() {
var optionSelected = $(this).find("option:selected");
semesterSelected = optionSelected.val();
console.log(semesterSelected);
$.ajax({
type: "POST",
cache: false,
url : "rate/units",
data: { sem : semesterSelected },
success: function(data) {
var obj = $.parseJSON(data);
var i = 0;
console.log(data.iyo);
$.each(obj, function() {
console.log(this[0]);
console.log(this[1]);
console.log(this[2]);
console.log(this[3]);
console.log(this[4]);
i++;
});
}
})
.done(function(data) {
alert('done');
})
.fail(function(jqXHR, ajaxOptions, thrownError) {
alert('No response from server');
});
});
});
</script>
回答by Antonio Carlos Ribeiro
You post data can be retrieved using Input:
可以使用 Input 检索您发布的数据:
public function show()
{
$semester = Input::get('sem');
return json_encode($semester);
}
To debug it you can:
要调试它,您可以:
public function show()
{
Log::info(Input::all());
$semester = Input::get('sem');
return json_encode($semester);
}
And then execute at the console
然后在控制台执行
php artisan tail
回答by Muhammad Shahzad
I'm using Laravel version 5.4.36 and I used this method to retrieve data from ajax request.
我使用的是 Laravel 5.4.36 版,我使用此方法从 ajax 请求中检索数据。
Controller:
控制器:
public function processData(Request $request){
$data = $request->all();
print_r($data);
echo $data['email'];
}
View:
看法:
function someMethod(prm){
$.get("{!! route('routename') !!}",{email:prm},function(res){
console.log('Response:',res);
});
}