gpt4 book ai didi

angularjs - MarkLogic 8 NodeJS Angular JSON 交易

转载 作者:搜寻专家 更新时间:2023-11-01 00:42:19 24 4
gpt4 key购买 nike

虽然我使用 Postman 通过 NodeJS 将数据 PUT 到 MarkLogic 8 没有问题,但使用 Angular 我无法让它工作。 req.body 返回 {}

在我尝试过的事情中,添加了这个:

app.use(bodyParser.urlencoded({
extended: true
}));

和:

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();
});

我的 Controller 中有以下功能:

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 中我有以下内容:

// 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!' });
});

最佳答案

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

app.use(bodyParser.json());

关于angularjs - MarkLogic 8 NodeJS Angular JSON 交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30369667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com