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

212
Visualizações
.Net-Core html form request redirects the page to related View page but with JavaScript website it does not redirecting to action view page

I would like to ask difference between calling Control's action via html FORM and JavaScript. If I use NetCore calls the Action Method and returns View() obj into View page.

But when I call the same Action, webpage stays in the same page.

For example, you have a Employee List page where all employees are listed and wanted to see on of the specific employees details in Emplpoyee Details page.

With html <form method='post' action ='EmployeeDetails'> </form> It works and browser opens the new page.

But with Javascript

function postSelectedCustomerData(path, method = 'post') {

let data = {
    SearchText: 'SearchText1',
    EmpName: '1',
    DealsOnly: true,
    PageNumber: 1
}

fetch("../Employee/EmployeeDetailsPost", {
    method: "POST",
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(data)
   }).then(res => {
    console.log("Request complete! response:", res);
 });
}

NOTE: I know I can redirect the page from fetch Return with window.location.href. But I want to know is there any way i can call action method with JavaScript and return to the action Method page?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you use standard <form> framework functionality, the server posts back to the client an HTTP response that's being interpreted by the browser as a new web page to be loaded.

When you skip this using a simple javascript fetch, the handling of the response it's all up to you. Just from a DOM point of view (let's suppose you only get '200' response!) you should identify the parent of the existing tree that will be replaced, remove said tree, insert the new one from the response.

fetch("../Employee/EmployeeDetailsPost", {
    method: "POST",
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(data)
    }).then(res => {
        let parent = document.getElementById('parent');
        let root = document.getElementById('root');
        root.remove();
        parent.append(res);
    });
}
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