【问题标题】:Access jquery data in laravel controller在 laravel 控制器中访问 jquery 数据
【发布时间】:2018-01-20 04:59:48
【问题描述】:

在我的 laravel 控制器中从 ajax 访问数据时遇到问题。 我的代码使用 Apache 在本地运行,但在使用 nginx 的服务器上运行(不知道这是否是问题) 如果我使用静态 SQL 请求执行此操作,它可以工作,但我需要动态访问数据。

http 路由没问题,但在控制器中没有获取数据。

我的控制器功能:

public function readHSauswahl(Request $request){

        $data = Message::where('name', '=', $request->title)->get();

        return response()->json($data);

我的路线:

Route::get('/readHSauswahl', 'AjaxController@readHSauswahl');

和 ajax 调用:

    $.ajax({
        type: 'get',
        url:'{!!URL::to('/readHSauswahl')!!}',
        dataType: "json",
        data:{'title':hochschule_name},
        success:function(data){
            console.log(data);
...

这可能与 nginx 配置有关??

server {
    listen 80;

    root /var/www/html/mydata/public;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name http://www.mydomain.de;

    location / {
        try_files $uri $uri/ /index.php?query_string;
    }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;         
        include fastcgi_params;
        }

}

编辑: 按照建议更改路线。仍然可以在本地工作,但不能在服务器上工作。

在控制器中试过:

$data1 = $_GET['title'];
//internal server error

$data = $request->input('title'); 
//same behaviour as before

【问题讨论】:

  • 似乎url:'{!!URL::to('/readHSauswahl')!!}', 没有产生正确的网址来点击。尝试一次硬编码的网址
  • 感谢您的评论。但这是正确的网址。在 chromes 网络日志中检查了它

标签: php jquery ajax laravel nginx


【解决方案1】:

最好给你的路线命名

Route::get('/readHSauswahl', 'AjaxController@readHSauswahl')->name('readhsa');

并在您的 ajax 设置 url 中使用 route() helper

$.ajax({
  type: 'get',
  url:'{{route('readhsa')}}',
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-14
    • 2023-03-22
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多