Tôi muốn biết tại sao việc đăng một chuỗi JSON đơn giản trong một /:parameter
là rất khó để khôi phục. Tôi đã theo nhiều ví dụ nhưng không tìm thấy bất cứ điều gì cụ thể.Đăng đối tượng JSON JSON vào NodeJs Phục hồi
Tôi có mã sau trong giao diện người dùng.
$("#btnDoTest").click(function() {
var jData = {
hello: "world"
};
var request = $.ajax({
url: "http://localhost:8081/j/",
async: false,
type: "POST",
data: JSON.stringify(jData),
contentType: "application/javascript",
dataType: "json"
});
request.success(function(result) {
console.log(result);
});
request.fail(function(jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
});
Tôi thành công trong việc gửi văn bản đơn giản nếu tôi nối thông số sau j/
. Nhưng những gì tôi muốn gửi là một đối tượng như thế này {hello:"world"}
và tái tạo lại nó trong nodeJS và làm việc với nó.
--edit:
This is my nodejs file
/* the below function is from restifylib/response.js */
var restify = require("restify");
/* create the restify server */
var server = restify.createServer({
});
server.use(restify.bodyParser({ mapParams: true }));
server.use(
function crossOrigin(req,res,next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
return next();
}
);
server.post('/j/', function (req, res, next) {
//res.send(201,"REceived body: "+JSON.stringify(req.params));
res.send(201,"REceived body: "+JSON.stringify(req.params));
return next();
});
var port = 8081;
server.listen(port);
console.log("Server listening on port " +port)
Bất kỳ trợ giúp sẽ được đánh giá cao nhờ.
0x
Cảm ơn, 'contentType: "application/x-www-form-urlencoded "'là những gì làm việc cho tôi quá. –