Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

918
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!