Запрос Ajax не будет работать, когда я выбираю базу данных. (Laravel) - PullRequest
0 голосов
/ 25 октября 2019

Код выше мой контроллер.

<?php

    namespace App\Http\Controllers;
    use DB;
    use Illuminate\Http\Request;

    class AjaxController extends Controller
    {
        public function post(Request $request){
          session_start();
          $select=DB::select('select empresa from users where username = su');

          $response = array(
              'status' => 'success',
              'msg' => $request->message,
          );
          return response()->json($response);

       }
    }

На мой взгляд, у меня есть только вход и функция:

$(document).ready(function(){
            var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
            $(".postbutton").click(function(){
                $.ajax({
                    /* the route pointing to the post function */
                    url: '/postajax',
                    type: 'POST',
                    /* send the csrf-token and the input to the controller */
                    data: {_token: CSRF_TOKEN, message:$(".getinfo").val()},
                    dataType: 'JSON',
                    /* remind that 'data' is the response of the AjaxController */
                    success: function (data) { 
                        $(".writeinfo").append(data.msg); 
                    }
                }); 
            });
       });    

<body>
    <input class="getinfo"></input>
    <button class="postbutton">Post via Ajax!</button>
    <div class="writeinfo"></div>   
</body>

Когда я пытаюсь сделать выбор в базу данных, запрос ajax не работает. Если бы запрос ajax работал, я бы использовал $ select вместо $ request.

1 Ответ

0 голосов
/ 25 октября 2019

Вы забыли добавить кавычки к значению имени пользователя:

$select=DB::select("select empresa from users where username = 'su'");
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...