【发布时间】:2013-05-31 02:49:01
【问题描述】:
我想知道为什么很难在/:parameter 中发布一个简单的 JSON 字符串来恢复。我遵循了很多例子,但没有找到任何具体的例子。
我在前端有如下代码。
$("#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);
});
});
如果我在j/ 之后连接参数,我可以成功发送简单文本。但我要发送的是像 {hello:"world"} 这样的对象,然后在 nodeJS 中重新构建它并使用它。
--编辑:
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)
任何帮助将不胜感激。
0x
【问题讨论】: