Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
how can I send post request in ajax to laravel route parameters

I want to toggle publish stat of my exams in laravel, I created a route for toggling and it worked, but I don't want to refresh the page every time I send a POST request so I created this function to send,

function togglePublish(id){
    let token = '@csrf';
    token = token.substr(42, 40);
    $.ajax({
        type: "post",
        url: `{{ route('togglePublish','${id}') }}`,
        data: `_token=${token}`
    });
}

I meant by this after the laravel compile the page, the URL will be https://localhost/toggle/${id} then javascript change the id variable to 1 for example, so the link be https://localhost/toggle/1 and that will work, but laravel returned

Missing required parameter for [Route: togglePublish] [URI: a/togglePublish/{exam}] [Missing parameter: id].

I think I have to add @csrf

the route

Route::post('/a/togglePublish/{exam:id}', [AdminExamController::class, 'togglePublish'])->name('togglePublish');

the togglePublish function in AdminExamController controller

public function togglePublish(Exam $exam)
{
    dd($exam);
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

in plus of what @vivekVaghela said, I've changed my ajax to

function togglePublish(id){
        let token = '@csrf';
        token = token.substr(42, 40);
        $.ajax({
            type: "post",
            url: `{{ url('/a/togglePublish/'.'${id}') }}`,
            data: `_token=${token}`,
            error: function(err) {
                console.log( $($(err.responseText)[1]).text() )
                debugger;
            }
        });
    }

EDIT: to use ajax you have to use the uncompressed script tag, as below

  1. JQuery Official website: Click me

  2. Select the version that you need (The latest stable version is recommended) and click on the uncompressed button enter image description here enter image description here Copy the script on the dialog box and past it on your code: enter image description here enter image description here


res
TypeError: $.ajax(...) is not a function?

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda