Question

Problem with ajax on server (in localhost is good)

Posted June 10, 2020 4k views
JavaScriptLaravel

Hello, I have a ajax in my localhost and is working good, but when I put it in the dopplet no is working. Any block? I must config anything more?

My ajax:

$.ajax({
          type: "post",
          url: url,
          data: {"_token": "{{ csrf_token() }}",
                  year: year, index: index
          },
          success: function(data) {

              $.each(data,function(key,value){
                var oBox=$('<div>').addClass('box').appendTo($('#gallery'));
                var oPic=$('<div>').addClass('pic').appendTo(oBox);
                $('<img>').attr('src', $(value).attr('src')).appendTo(oPic);
});

My Laravel controller:

public function gallery(Request $request)
    {
        $json = Photo::where('year', '=', $request->year)->skip(20 * $request->index)->take(20)->get();

        if (!$json->isEmpty()) {

            foreach ($json as $data)
            {
                $elements[] = ['src' => '../img/gallery/'.$data->year.'/'.$data->filename.'.'.$data->extension, 'download' => '../img/gallery/'.$data->year.'/download/'.$data->filename.'.'.$data->extension];
            }

                $json = response()->json($elements);
        }

        return $json;
    }

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there @alquilariumdev,

The code that you’ve shared looks good. I would recommend checking your web console for any errors, to do that just right click on your website, then clock on ‘Inspect element’, after that go to the console tab. After that try to trigger the AJAX request and see if any errors pop up in the console.

Feel free to share the errors here so I could try to advise you further.

Regards,
Bobby