Witam, wg dokumentacji Express 4.x .json() ma wbudowany JSON.stringify():
"res.json([body])
Sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using JSON.stringify().
The parameter can be any JSON type, including object, array, string, Boolean, or number, and you can also use it to convert other values to JSON, such as null, and undefined (although these are technically not valid JSON)."
Tymczasem w express wysyłam:
app.get("/load", function(req, res) {
res.status(200).json({response: "ok"});
});
A w ajax odbieram dane:
$.ajax({
type: 'GET',
url: 'https://example/load',
contentType: 'application/json; charset=UTF-8',
dataType: 'json'
}).done(function(data, textStatus, xhr) {
console.log(xhr.status + " " + textStatus);
console.log(data);
}).fail(function(xhr, textStatus) {
console.log(xhr.status + " " + textStatus);
});
Wszystko wskazuje na to że console.log(data) powinien wyświetlić: {"response":"ok"} ,okazuję się jednak, że dostaję w konsoli: Object { response: "ok" }. Dopiero gdy ręcznie przekonwertuję obiekt przez JSON.stringify() otrzymuję spodziewany rezultat.