Laravel 将 Json 发送到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29715539/
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
Laravel send Json to Controller
提问by Seif wares
I just start using Laravel 5 and i'm facing a problem to send Ajax json data from the view to controller ..
我刚开始使用 Laravel 5,但在将 Ajax json 数据从视图发送到控制器时遇到了问题。
This is my routes.php :
这是我的 routes.php :
Route::post('ticket','TicketController@store');
Route::get('ticket', 'TicketController@index');
and this is the controller :
这是控制器:
public function store()
{
return Response::json(Input::get('ticketname'));
}
and Finally for the View i have this simple example to pass just one input :
最后对于视图,我有这个简单的例子来传递一个输入:
<script>
$(document).ready(function() {
$('#go').on("click",function(){
var ticketname= ($('.tick_name').val());
$.ajax({
url: 'ticket',
type: 'POST',
data: {ticketname:$('.tick_name').val()},
dataType: 'json',
success: function(info){
console.log(info);
}
});
});
});
**IM ALWAYS GETTING THIS ERROR : localhost:8000/ticket
**我总是收到这个错误:本地主机:8000/票
500 Internal Server Error on jquery.js line 4 ..**
jquery.js 第 4 行 500 内部服务器错误 ..**
CAN anyone help please !
任何人都可以帮忙吗!
回答by lukasgeiter
Response
is an alias in the global namespace. Since you're current namespace is App\Http\Controllers
you have to import the class:
Response
是全局命名空间中的别名。由于您是当前的命名空间,因此App\Http\Controllers
您必须导入该类:
use Response;
Or prepend a backslash:
或者在前面加上反斜杠:
return \Response::json(Input::get('ticketname'));