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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:22:42  来源:igfitidea点击:

Laravel send Json to Controller

javascriptphpajaxjsonlaravel

提问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

Responseis an alias in the global namespace. Since you're current namespace is App\Http\Controllersyou have to import the class:

Response全局命名空间中的别名。由于您是当前的命名空间,因此App\Http\Controllers您必须导入该类:

use Response;

Or prepend a backslash:

或者在前面加上反斜杠:

return \Response::json(Input::get('ticketname'));