Question

Problem with ajax on server (in localhost is good)

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;
    }

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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