Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

116
Vistas
Return a view from an ajax call

I have 0 experience in Ajax, and now I'm trying to get an html table to return on an ajax call.

As a result, I get the error

jquery-3.6.0.min.js:2 POST http://test.loc/%7B%7B%20url('blog/articles')%20%7D%7D 404 (Not Found)

I understand that there are still a lot of mistakes, so don't judge too much, and help in any way you can :)

Route:

Route::post('blog/articles', 'App\Http\Controllers\BlogController@articles');

Ajax on calling page:

function getBlogLists(category_id) {
  var category_id = category_id;
  $.ajax({
      url: "{{ url('blog/articles') }}",
      type: 'POST',
      data: { 'category_id': category_id },
      datatype: 'html',
      success: function(data) {
          console.log('success');
          console.log(data);
          
            document.querySelectorAll('.blog-filter__item').forEach(el => {
              el.addEventListener('click', () => {
                document
                  .querySelector('.blog-filter__item.active')
                  .classList.remove('active');
                el.classList.add('active');
            
                  var dataFilter = $(el).attr('data-filter');
            
                  if (dataFilter == 'all') {
                    $('.blog-list').show();
                  }
                  else {
                    $('.blog-list').hide()
                    $(dataFilter).show()
                  }
              });
            });
          
      },
  });
}

//on page load
getBlogLists("{{ $category->id }}");

Controller:

public function articles() {
        $input = Request::all();
        if(Request::isMethod('post') && Request::ajax()) {
            if($input['category_id']) {
                $articles = Article::select('select * from blog_categories where blog_category_id = ?', array($input['category_id']));
                $returnHTML = view('blog.articles')->with('articles', $articles)->render();
                return response()->json( array('success', 'html'=>$returnHTML) );
    
            }
        }   
    }

View:

@foreach($articles as $index => $article)
    <div class="blog-list category_{{ $article->blog_category_id }}">
        @if ($index % 2 === 1)

        <div class="blog-article blog-article--right">
                <h2 class="blog-article_title">{{ $article->title }}</h2>
            </div>
        @else

            <div class="blog-article blog-article--left">
                <h2 class="blog-article_title">{{ $article->title }}</h2>
            </div>
        @endif
    </div>
@endforeach
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have this error because you have a problem in your URL.

function getBlogLists(category_id) {
  var category_id = category_id;
  $.ajax({
      url: "{{ url('blog/articles') }}",

Here, the url is literally {{ url('blog/articles') }}, I mean, as a string.

You are sending the request to http://test.loc/{{ url('blog/articles') }}, which once encoded gives http://test.loc/%7B%7B%20url('blog/articles')%20%7D%7D.

That's why you are getting a 404 error (not found), obviously this url doesn't exist.

First, remove the url variabilization:

function getBlogLists(category_id) {
  var category_id = category_id;
  $.ajax({
      url: "http://test.loc/blog/articles", //or whatever your url is

Then in your controller, you just have to return the HTML and it will be inside data in your javascript success callback.

Do a simple test first:

public function articles() {
    return "<h1>HelloWorld</h1>";
}

If it works with this simple "hello world", it will work with the view you are rendering as well.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda