![](/img/trans.png)
[英]I've created a node app through cloud 9, but how do I connect my app with my website?
[英]How do I connect service to a Cloud Foundry app?
我是 IBM 雲的新手,我正在嘗試構建一個應用程序,我可以在其中編寫文本、按下按鈕,然后服務語氣分析器分析該文本並返回 JSON,以便我可以顯示它。
我創建了上述服務的一個實例,並使用服務上的“連接”選項卡將其連接到我的應用程序(工具鏈)。
我的應用程序的 app.js 文件中也有此代碼:
const ToneAnalyzerV3 = require('ibm-watson/tone-analyzer/v3');
const { IamAuthenticator } = require('ibm-watson/auth');
const toneAnalyzer = new ToneAnalyzerV3({
version: '2019-10-10',
authenticator: new IamAuthenticator({
apikey: [API key found in service credentials],
}),
url: [API url found in service credentials],
});
app.get('/', function(req, res) {
res.render('index');
});
app.post('/api/tone', async function(req, res, next) {
try {
const { result } = await toneAnalyzer.tone(req.body);
res.json(result);
} catch (error) {
next(error);
}
});
問題是,當我對我的 javascript 進行以下調用時:
$.post( "/api/tone", {text: textInput}, function(data){
console.log(data);
});
我收到錯誤:500(內部服務器錯誤)。
有誰知道我做錯了什么?
問題是您正在發送req.body
以進行語氣分析。 如果您查看 API 文檔 - https://cloud.ibm.com/apidocs/tone-analyzer?code=node#tone - 您會看到您只需要發送
const toneParams = {
toneInput: { 'text': text },
contentType: 'application/json',
};
我非常懷疑req.body
有一個toneInput
字段,如果它確實有contentType
它可能不會被設置為允許的值之一。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.