除了res.json里面的值必须是json外,还有什么区别呢?
光阴似箭催人老,日月如移越少年。
Difference between res.send and res.json in Express.js 搬運一個stackoverflow上的答案
下面是翻譯過來的高票答案:
當傳遞物件或陣列時,這兩個方法是相同的,但是res.json()也會轉換非物件,如null和undefined,這些無效的JSON。 res.json()也会转换非对象,如null和undefined,这些无效的JSON。
res.json()
null
undefined,這些無效的JSON。 res.json()也会转换非对象,如null和undefined,这些无效的JSON。
undefined
该方法还使用json replaceacer和json spaces的设置,因此您可以使用更多选项格式化JSON。 例如:
json replaceacer
json spaces
app.set('json spaces', 2); app.set('json replacer', replacer);
传递给JSON.stringify()类似:
JSON.stringify()
JSON.stringify(value, replacer, spacing); // value: 需要格式化的对象 // replacer: stringify 时如何转化属性的规则 // spacing: 锁紧的空格数量
res.json方法的中res.send部分没有的代码:
res.json
res.send
var app = this.app; var replacer = app.get('json replacer'); var spaces = app.get('json spaces'); var body = JSON.stringify(obj, replacer, spaces);
最终它使用res.send
this.charset = this.charset || 'utf-8'; this.get('Content-Type') || this.set('Content-Type', 'application/json'); return this.send(body);
Difference between res.send and res.json in Express.js 搬運一個stackoverflow上的答案
下面是翻譯過來的高票答案:
當傳遞物件或陣列時,這兩個方法是相同的,但是
res.json()
也會轉換非物件,如null
和undefined,這些無效的JSON。
res.json()
也会转换非对象,如null
和undefined
,这些无效的JSON。该方法还使用
json replaceacer
和json spaces
的设置,因此您可以使用更多选项格式化JSON。 例如:传递给
JSON.stringify()
类似:res.json
方法的中res.send
部分没有的代码:最终它使用
該方法還使用res.send
json replaceacer
和json spaces
的設置,因此您可以使用更多選項來格式化JSON。 例如:🎜 🎜傳遞給JSON.stringify()
類似:🎜 rrreee 🎜res.json
方法的中res.send
部分沒有的程式碼:🎜 rrreee 🎜最終它使用res.send
發送請求🎜 rrreee