2

I have a problem with an ajax call. The url that i need to get the data is:

localhost/public/getCode?course_id=1&task_id=1

My ajax call is:

function getCode() {
                $.ajax({
                    type: "GET",
                    dataType: 'json',
                    url: "{{action('CodeEditorController@getCode',['course_id'=>$course,'task_id'=>$maintask])}}",
                    success: function (data) {
                        console.log(data);
                    }
                });
            }

But the data returned is empty.

Edit: getCode function:

public function getCode(Request $request)
{
    $code=Code::where('user_id',$user->id)->where('main_task_id',$request->input('task_id'))->first()->code;
    $response = [
        'data' => $code
    ];

    return response()->json($response, 200);
}

What is the issue with my ajax code?

Thanks!

4
  • Can you add the code in your getCode method? Commented Jan 3, 2017 at 19:23
  • Any errors returned or just empty result set? Try adding error logging for ajax request after your success statement. error: function(jqXHR, textStatus, errorThrown) { $content.fadeOut(200); console.log(JSON.stringify(jqXHR)); console.log("AJAX error: " + textStatus + ' : ' + errorThrown); Commented Jan 3, 2017 at 19:32
  • @AndrewNolan no error, just empty response. With postman it returns the data, but with my ajax call it wont. I think its a problem with my ajax code Commented Jan 3, 2017 at 19:34
  • 1
    hmmm, maybe try adding your parameters as a data section in ajax call instead of attached to url. data: {'task_id' : $maintask, 'course_id' : $course} Commented Jan 3, 2017 at 19:37

1 Answer 1

4

One way to do that is to use data for options:

data: {
    'course_id': {{ $course }},
    'task_id': {{ $maintask }} 
},

To get values in controller you can just use request('course_id') and request('task_id')

Also it's a really bad idea to use Blade/PHP to build JS. You should use hidden inputs or something to pass data to JS.

Sign up to request clarification or add additional context in comments.

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.