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