Report this

What is the reason for this report?

Problem with ajax on server (in localhost is good)

Posted on June 10, 2020

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


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!

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.

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.