Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

917
Vistas
400 error when send JSON data to WordPress Admin Ajax with Fetch API

I use Fetch Api with WordPress admin-ajax.php.

  1. Send data as URLSearchParams. It's WORKING.
    let data = {
        action: 'my_action',
    }
    
    fetch( ajaxurl, {
        method: 'POST',
        credentials: 'same-origin',
        body: new URLSearchParams(data)
    });
  1. Send data as FormData. It's WORKING.
    let data = New FormData();
    data.append('action', 'my-action');
    
    fetch( ajaxurl, {
        method: 'POST',
        credentials: 'same-origin',
        body: data
    });
  1. Send data as JSON . It's NOT WORKING, 400 ERROR.
    let data = {
        action: 'my_action',
    }
    
    fetch( ajaxurl, {
        method: 'POST',
        credentials: 'same-origin',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    });

I'm curious why it got 400 error? anything I miss?

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

0

change your fetch login as below and try. Since you are specifying 'Content-Type' : 'json' , no need to stringify the json.

    fetch( ajaxurl, {
        method: 'POST',
        credentials: 'same-origin',
        headers: {
          'Content-Type': 'application/json'
        },
        body: data
    });
over 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda