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

220
Visualizações
Pass JSON to aspx webmethod 500 Internal Server Error

I am trying to learn JSON and jQuery and before I delve into anything too complex I thought I would try a simple example to get me up to speed.

I have a button that when clicked I would like to pass a JSON object through to a WebMethod, perform some string operations on it and return to the client side.

However when I click the button nothing happens, the browser console shows the following error:

POST http://localhost:53388/myfolder/foo/aspx/setrecord 500 (Internal Server Error)

Any help will be greatly appreciated

jQuery

$('#SaveButton').click(function () {
    var jsonObj = { "data":[{ "id": 1, "name": "Zippy" }, { "id": 2, "name": "George" }, { "id": 3, "name": "Bungle" }, { "id": 4, "name": "geoffrey"}] }
    jsonObj = JSON.stringify(jsonObj);
    SaveNewOrder(jsonObj);
})

function SaveNewOrder(jsonObj) {
    $.ajax({
        type: "POST",
        url: "foo.aspx/setrecord",
        data: jsonObj,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            alert(response.d);
        }
    });
}
function OnSuccess(response) {
    $('#OutputDiv').html(response.d);
}

Classes and WebMethod

public class ListOfDestinationColumns
{
    public List<DestinationColumn> data { get; set; }
}

public class DestinationColumn
{

    public int id { get; set; }
    public string name { get; set; }
}

[WebMethod]
public static string setrecord(string jsonObj)
{
    ListOfDestinationColumns Destinations = new JavaScriptSerializer().Deserialize<ListOfDestinationColumns>(jsonObj);

    string output = "Start<br/>";
    foreach (var item in Destinations.data)
    {
        output += string.Format("id: {0}, name: {1}<br />", item.id.ToString(), item.name);
    }
    output += "End";
    return output;
}
about 4 years ago · Santiago Trujillo
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