3

Hey im trying to send my ajax json input to my server, but it wont work.

At sending the json(stringified) to my server, my server is crying with: SyntaxError: Unexpected end of input at Object.parse (native)

But when im sending the same json via Postman, no error appears.

My ajax:

       $.ajax({
            method: "POST",
            url: "/new",
            data: {ort: park[0], activity: activity, datum: date, teilnehmerzahl: teilnehmerzahl, schwierigkeit : schwierigkeit, dauer : dauer, time : time, treffpunkt : treffpunkt},
            dataType: "json",
            success: function (data) {
                alert(data);
            }
            , error: function (jqXHR, textStatus, err) {
                alert('text status ' + textStatus + ', err ' + err)
            }
        });

Typical stringified json:

{"ort":"Bayerischer Wald","activity":"Klettern","datum":"17.09.2015","teilnehmerzahl":"2","schwierigkeit":"Anfänger","dauer":"1h","time":"12:00","treffpunkt":"Hier"}

My client:

app.post('/new', jsonParser, function(req,res){
    var test = JSON.stringify(req.body);
    fs.readFile('./views/neueGruppe.ejs', {encoding: 'utf-8'}, function(err, filestring){
        if(err){
            throw err;
        }
        else{
            var options = {
                host: 'localhost',
                port: 3000,
                path: '/new',
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Content-Length': test.length
                }
            }

            var req = http.request(options, function(response) {
                response.on('data', function (chunk) {

                });
            });

            req.on('error', function(e) {
                console.log('problem with request: ' + e.message);
            });

// write data to request body
            req.write(test);
            req.end();
        }
    });
});

My server:

rest.post("/new", jsonParser, function(req,res){

    var data = {users:
        [
            {id: 1, name: "Peter"},
            {id: 2, name: "Jessica"}
        ]}


    console.log(req);
    res.json(data);


});

When i change the Content-Type on my client from Json to text, no error appears, but no data was send either. Its only happening when i try to send it as json, but even jsonlint says that its valid json...

Lukas S
  • 315
  • 1
  • 3
  • 15

2 Answers2

3

Use the JSON.stringify Method To Send the Correct Request.

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.

Syntax

JSON.stringify(value[, replacer[, space]])

Source: Mozilla Contributors

JSON.stringify() converts a value to JSON notation

$.ajax({
    method: "POST",
    url: "/new",
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({ort: park[0], activity: activity, datum: date, teilnehmerzahl: teilnehmerzahl, schwierigkeit : schwierigkeit, dauer : dauer, time : time, treffpunkt : treffpunkt}),
    dataType: "json",
    success: function (data) {
        alert(data);
    },
    error: function (jqXHR, textStatus, err) {
        alert('text status ' + textStatus + ', err ' + err)
    }
});
SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
Akhilesh Singh
  • 1,724
  • 2
  • 19
  • 35
2

To correctly send your JSON in request body, use JSON.stringify:

       $.ajax({
            method: "POST",
            url: "/new",
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ort: park[0], activity: activity, datum: date, teilnehmerzahl: teilnehmerzahl, schwierigkeit : schwierigkeit, dauer : dauer, time : time, treffpunkt : treffpunkt}),
            dataType: "json",
            success: function (data) {
                alert(data);
            }
            , error: function (jqXHR, textStatus, err) {
                alert('text status ' + textStatus + ', err ' + err)
            }
        });

More information can be found here: jQuery posting valid json in request body

Community
  • 1
  • 1
ischenkodv
  • 4,205
  • 2
  • 26
  • 34