2015-09-07 74 views
3

嘿即時嘗試發送我的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 ...

回答

3

使用JSON.stringify方法發送正確的請求。

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()將數值轉換爲JSON符號

$.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) 
 
    } 
 
});

2

要正確地發送您的JSON的請求體,使用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) 
 
      } 
 
     });

更多信息可以在這裏找到:jQuery posting valid json in request body