2016-12-09 67 views
1

使用此代碼我創建了一個函數,該函數創建一些JSON數據並將其發送到server.js(我的節點服務器)。發送到節點服務器時發生更改的JSON對象

function deleteEmail(i) { 
    emailObj.splice(i, 1); 
    var general = {}; 
    var table = [] 
    general.table = table; 
    for (var i = 0; i < emailObj.length; i++) { 
     var dataHtml = emailObj[i].html; 
     var html = { 
     "html": dataHtml 
     } 
     general.table.push(html); 
    } 
    console.log(JSON.stringify(general)); 
    $.post("email2", general); 
    } 

server.js內的代碼來捕獲JSON數據是這樣的:

app.post("/email2", function(req, res){ 
    console.log(req.body); 
}); 

這裏是由功能創建併發送至server.js JSON數據:

{"table":[{"html":"<b>ID email:</b> #1481145671503<br><b>Form journey:</b> General<br><b>Work Request Title:</b> <br><b>Data Request:</b> <br><b>Request By:</b> <br><b>Department:</b> <i>not specified</i><br><b>Business Owner:</b> <br><b>Contact Details:</b> <br><b>Request Overview:</b> <br><b>Platform Impacted:</b> undefined<br><b>Business Objectives:</b> <br><b>Business Benefits Justifications:</b> <br><b>Project Drive:</b> <br><b>Additional Information:</b> <br>"},{"html":"<b>ID email:</b> #1481214851188<br><b>Date:</b> Thu Dec 08 2016 16:34:11 GMT+0000 (GMT)<br><b>Form journey:</b> General<br><b>Work Request Title:</b> <br><b>Data Request:</b> <br><b>Request By:</b> <br><b>Department:</b> <i>not specified</i><br><b>Business Owner:</b> <br><b>Contact Details:</b> <br><b>Request Overview:</b> <br><b>Platform Impacted:</b> undefined<br><b>Business Objectives:</b> <br><b>Business Benefits Justifications:</b> <br><b>Project Drive:</b> <br><b>Additional Information:</b> <br>"}]} 

這裏是服務器正在接收的JSON數據:

{ 'table[0][html]': '<b>ID email:</b> #1481145671503<br><b>Form journey:</b> General<br><b>Work Request Title:</b> <br><b>Data Request:</b> <br><b>Request By:</b> <br><b>Department:</b> <i>not specified</i><br><b>Business Owner:</b> <br><b>Contact Details:</b> <br><b>Request Overview:</b> <br><b>Platform Impacted:</b> undefined<br><b>Business Objectives:</b> <br><b>Business Benefits Justifications:</b> <br><b>Project Drive:</b> <br><b>Additional Information:</b> <br>', 
    'table[1][html]': '<b>ID email:</b> #1481214851188<br><b>Date:</b> Thu Dec 08 2016 16:34:11 GMT+0000 (GMT)<br><b>Form journey:</b> General<br><b>Work Request Title:</b> <br><b>Data Request:</b> <br><b>Request By:</b> <br><b>Department:</b> <i>not specified</i><br><b>Business Owner:</b> <br><b>Contact Details:</b> <br><b>Request Overview:</b> <br><b>Platform Impacted:</b> undefined<br><b>Business Objectives:</b> <br><b>Business Benefits Justifications:</b> <br><b>Project Drive:</b> <br><b>Additional Information:</b> <br>' } 

它爲什麼這樣做,我怎麼能發送它也保持完全相同的格式和數據,並解釋它由server.js。

謝謝

+0

你可以檢查幾件事情 - 明確設置連續在發送請求時,在jquery中將ent類型轉換爲'application/json',並檢查node.js應用程序中的body parser中間件。看來這個問題最有可能在這兩個地方之一。 –

回答

0

你永遠不會發送JSON數據到服務器。

console.log(JSON.stringify(general)); 

在上線你:

  1. 採取general,JavaScript對象
  2. 將其轉換爲JSON
  3. 登錄它
$.post("email2", general); 

在上面的行中,您將general(仍然不是JSON!)傳遞給$.post

當您通過post對象,jQuery將其轉換爲application/x-www-form-urlencoded編碼數據併發送。


如果你想發送JSON,那麼你需要:

  • 說你要發送JSON
  • 實際發送JSON

這樣的:

$.ajax({ 
    url: "email2", 
    method: "POST", 
    data: JSON.stringify(general), 
    contentType: "application/json" 
}); 
+0

謝謝!相當新的ajax,我會知道下一次。 –