• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

92
Vistas
Enviando cadenas simples con ajax

Estoy tratando de enviar una cadena simple usando una solicitud de publicación de Ajax:

jQuery

 var user = { "name": name, "country": country, }; $.ajax({ type: 'POST', traditional: true, url: 'Default.aspx/Submit', data: JSON.stringify(user), contentType: "application/json; charset=utf-8", dataType: "text", success: function (msg) { alert("Success!"); }, error: function () { alert("Error!"); } });

ASP.NET

 [WebMethod] public static bool Submit(string json) { // do something return true; }

Lo puse:

  • tradicional = verdadero para evitar la codificación recursiva (no estoy seguro si es necesario)
  • los datos son una string , no un object
  • tipo de datos es text no json

pero todavía recibí este error del servidor:

llamada de servicio web no válida, valor faltante para el parámetro: json

Supongo que esto se debe a que no encuentra un nombre coincidente para el parámetro "json". Pero no entiendo cómo tengo que nombrar el parámetro de función si estoy enviando una cadena simple.

over 3 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Este es el código que uso para publicar en una base de datos usando AJAX.

// Variable para retener solicitud var solicitud;

// Enlace al evento de envío de nuestro formulario $("#form1").submit(function(event){

 // Abort any pending request if (request) { request.abort(); } //local variables var $form = $(this); // select and cache all the fields var $inputs = $form.find("input, select, button, textarea"); // Serialize the data in the form var serializedData = $form.serialize(); // Let's disable the inputs for the duration of the Ajax request. // Note: we disable elements AFTER the form data has been serialized. // Disabled form elements will not be serialized. $inputs.prop("disabled", true); // Fire off the request to php file request = $.ajax({ url: yourdomain-asp-page, type: "post", data: serializedData }); // Callback handler that will be called on success request.done(function (response, textStatus, jqXHR){ //replace form with feedback text // Callback handler that will be called on failure request.fail(function (jqXHR, textStatus, errorThrown){ // Log the error to the console console.error( "The following error occurred: "+ textStatus, errorThrown ); }); // Callback handler that will be called regardless // if the request failed or succeeded request.always(function () { // Reenable the inputs $inputs.prop("disabled", false); }); // Prevent default posting of form event.preventDefault();

});

over 3 years ago · Santiago Trujillo 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda