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

155
Visualizações
Cannot send data from aspx file to code behind using $.ajax({ type: "POST", using VS2017 C#

I'm using web-forms to collect data from a form and then send the data to the code-behind to send to an API. Using a button I'm calling a JavaScript method which collates the data and then sends to my aspx.cs file to send to the API. The Html code for the button is

<button class="search btn" ID="btnSearch" onclick="searchApi(); return false;"><i class="fas fa-search"></i>Search</button>

This runs the searchAPI() function which works and creates a concatenated string called SearchData. The Javascript code looks like this

var searchString = JsonData;
var trimsearchString = searchString.replace(/,/g, '');

$.ajax({
   type: "POST",
   url: 'Default.aspx/GetApi',
      data: searchString,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (data) {
          alert('success');
                },
                error: function (errordata) {
                    console.log(errordata);
                    alert(errordata);
                }
    });

The method GetAPI in my Default.aspx.cs file is never called. The method code is

[System.Web.Services.WebMethod]
public static void GetApi(string searchData)    
{...

The success: function (data) returns success but the code behind method is never called, can someone please tell me what I am missing.

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

0

fix the ajax data, it seems that method with this parameter can' t be found

$.ajax({
   type: "POST",
   url: 'Default.aspx/GetApi',

      data: { searchData: trimsearchString},
      //or if it is still problem, you can even try
       data: JSON.stringify( { searchData: trimsearchString}),

      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (data) {
          alert(data);
                },
                error: function (errordata) {
                    console.log(errordata);
                    alert(errordata);
                }
    });

and your webmethod should return something

[WebMethod]
public static string GetApi(string searchData)    
{
   return searchData
}
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