2

I have two routes defined like this :

Route::get('directeur/qualite', 'Directeur\qualiteController@index')->name('qualite');
Route::get('directeur/qualite/{texte}','Directeur\qualiteController@getResultOfQuestion')->name('getResultQuestion');

My Controller have this function :

public function getResultOfQuestion(){
    $texte=Input::get('texte');
    $data = DB::table('questions')->where('texte','=',$texte)->value('code');
    return['data'=>$data];
}

And I'm doing a request using Ajax like this :

$.ajax({
         type: 'GET',
         url: '/emagine/projet1613/public/directeur/qualite/',
         data: {
               texte: encodeURIComponent(str)
               },
         success: function (data) {
                console.log(data);
                },
         error: function () {
                 alert('La requête n\'a pas abouti');
                 }
         });

I would like to get the result of the function defined in the Controller but I can not do it. What am I doing wrong?

1
  • any particular error thow in above code Commented Jan 20, 2017 at 11:12

2 Answers 2

1

Just try this

Controller

public function getResultOfQuestion($texte){
    $data = DB::table('questions')->where('texte','=',$texte)->value('code');
    return response()->json(array('data' => $data));
}

AJAX request

$.ajax({
        type: 'GET',
         url: '/emagine/projet1613/public/directeur/qualite/'+encodeURIComponent(str),
         success: function (data) {
                console.log(data);
                },
         error: function () {
                 alert('La requête n\'a pas abouti');
                 }
     });
Sign up to request clarification or add additional context in comments.

3 Comments

How do you use the helper URL in the javascript?
you have kept in separate js file? then i just edit my answer try that
@Erylis then please upvote and accept my answer so it help others to find it useful
0

You should return response:

return response()->json(compact('data'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.