Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node js cant parse stringified json

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...

like image 628
Lukas S Avatar asked Jan 19 '26 04:01

Lukas S


1 Answers

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)
    }
});
like image 146
Akhilesh Singh Avatar answered Jan 20 '26 17:01

Akhilesh Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!