嘿即時嘗試發送我的ajax json輸入到我的服務器,但它不會工作。節點js不能解析字符串化json
在發送JSON(字符串化)到我的服務器,我的服務器用哭泣: 語法錯誤:在Object.parse(原生)輸入 意外結束
但是通過郵路時,即時通訊發送相同的JSON,沒有錯誤出現。
我的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)
}
});
典型的字符串化JSON:
{"ort":"Bayerischer Wald","activity":"Klettern","datum":"17.09.2015","teilnehmerzahl":"2","schwierigkeit":"Anfänger","dauer":"1h","time":"12:00","treffpunkt":"Hier"}
我的客戶:
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();
}
});
});
我的服務器:
rest.post("/new", jsonParser, function(req,res){
var data = {users:
[
{id: 1, name: "Peter"},
{id: 2, name: "Jessica"}
]}
console.log(req);
res.json(data);
});
當我改變從Json的我的客戶端上的內容類型爲文本,沒有出現錯誤,但沒有數據被髮送。它只有當我嘗試把它發送爲JSON發生的事情,但即使jsonlint表示,其有效的JSON ...