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

324
Views
jQuery ajax POST, error 'SyntaxError: Unexpected end of JSON input'

Im sure im missing something simple, but so far struggling to see what...

I am wanting to POST some form field entries to a server in JSON format. I am currently using:

$(document).ready(function () {
    $("#newEnquiery").submit(function (event) {     
        event.preventDefault(); 
        $.ajax({
            url: 'SERVER-URL-HERE',
            type: 'POST',
            data: JSON.stringify({ "name": $('#name').val(), "emailAddress" : $('#emailAddress').val(), "address" : $('#address').val() }),
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function(data, textStatus, jQxhr){
                console.log('Sent');
            },
            error: function( jqXhr, textStatus, errorThrown ){
                console.log('fail');
                console.log(errorThrown);
            }
        });
    });
});

The error that I am being shows in the console is; SyntaxError: Unexpected end of JSON input

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

$.ajax({
    url: "https://jsonplaceholder.typicode.com/posts",
    type: "POST",
    data: JSON.stringify({
        name: "test",
        emailAddress: "test",
        address: "test",
    }),
    contentType: "application/json",
    dataType: "json",
    success: function (data, textStatus, jQxhr) {
        console.log(data);
    },
    error: function (jqXhr, textStatus, errorThrown) {
        console.log("fail");
        console.log(errorThrown);
    },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

contentType has to be application/json and you should pass a plain json into the JSON.stringify method into the data attribute. So the code has to be:

    $(document).ready(function () {
    $("#newEnquiery").submit(function (event) {     
        event.preventDefault(); 
        $.ajax({
            url: 'SERVER-URL-HERE',
            type: 'POST',
            data: JSON.stringify({ name: $('#name').val(), emailAddress : $('#emailAddress').val(), address : $('#address').val() }),
            contentType: 'application/json',
            dataType: 'json',
            success: function(data, textStatus, jQxhr){
                console.log('Sent');
            },
            error: function( jqXhr, textStatus, errorThrown ){
                console.log('fail');
                console.log(errorThrown);
            }
        });
    });
});
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!