首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MarkLogic 8 NodeJS角JSON事务

MarkLogic 8 NodeJS角JSON事务
EN

Stack Overflow用户
提问于 2015-05-21 09:24:26
回答 1查看 106关注 0票数 2

虽然我没有问题使用邮递员把数据通过NodeJS到MarkLogic 8,但使用角度,我无法使它工作。req.body返回{}

我尝试过的其中一件事是添加以下内容:

代码语言:javascript
复制
app.use(bodyParser.urlencoded({
  extended: true
}));

和:

代码语言:javascript
复制
app.use(function (req, res, next) {

    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:9040');

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);

    // Pass to next layer of middleware
    next();
});

我的控制器中有以下功能:

代码语言:javascript
复制
function putFabriek($fabriek, $http) {

    var mlUrl = 'http://localhost:3000/fabriek';
    console.log($fabriek);

    $http({
        method: 'PUT',
        url: mlUrl + '?id=' + $fabriek.id,
        headers: {'Content-Type': 'application/json'},
        data: JSON.stringify($fabriek)
    }).success(function(data, status) {
        console.log($http.succes);
    }).error(function(data, status) {
        console.log($http.error);
    });     
}; 

在NodeJS中,我有以下内容:

代码语言:javascript
复制
// Fabrieken Puter
app.put('/fabriek', function (req, res) {
    var inhoud = req.body;
    var fabId  = req.query.id;
    console.log(inhoud);
    db.documents.write(
        { uri: ('/fabrieken/' + fabId + '.json'),
          collections: ['examples', 'metadata-examples'],
          contentType: 'application/json',
          content: inhoud
        })
        .result(null, function(error) {
          console.log(JSON.stringify(error));
        });
    res.json({ message: 'Fabriek is updated!' });    
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-21 09:27:00

您正在发送JSON,因此需要添加相关的JSON body解析器方法。

代码语言:javascript
复制
app.use(bodyParser.json());
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30369667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档