gpt4 book ai didi

javascript - 无法以 Angular 读取 json 响应

转载 作者:行者123 更新时间:2023-12-03 07:38:53 27 4
gpt4 key购买 nike

js 和 node.js,我在 json 响应方面遇到了一些麻烦。我有一个执行 REST api 获取的 node.js 服务器,并且 json 结果必须发送到 Angular 客户端,但是当我尝试时要读取该 json 文件,我获得了 [Object,Object]。我无法弄清楚我缺少什么。感谢您帮助我。来自 REST API 的 Json 文件响应:

[ { cityPK: { name: 'Lodi', region: 'Lombardia' } },
{ cityPK: { name: 'Frosinone', region: 'Lazio' } },
{ cityPK: { name: 'Roma', region: 'Lazio' } },
{ cityPK: { name: 'Tivoli', region: 'Lazio' } } ]

node.js 文件:

var http = require('http');
var request = require('request');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

app.post('/login',function(req,res){
console.log("sono nella post "+req.body.username);
var uid = req.body.username;
var upwd = req.body.password;
res.setHeader('Content-Type', 'application/json');
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Credentials", true);



request({
url: "http://localhost:8080/RestServiceAdmin/webresources/entities.city",
method: "GET",
json: true ,
headers:[{'content-type': 'application/json'}]
}, function (error, response, body){
if(!error & response.statusCode === 200){
res.json(body);
}
});
});
app.listen(9080,function(){
console.log('Server running at http://127.0.0.1:9080/');
});

Angular 客户端:

 var app = angular.module('myApp',[]);
app.controller('myCtrl',function($scope,$http){
$scope.check = function(data){
var id = data.form.id;
var pwd = data.form.password;
console.log("utente è "+id+",password è "+pwd);
var msg = {username: id,password: pwd};
$http({
url:'http://localhost:9080/login', //senza niente qui,metti * in app.post()
method:"POST",
data: $.param(msg),
responseType: "application/json",
headers: {'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}})
.then(function(response){

console.log("Server response "+response.data);
});
};
});

我认为“response.data”应该给我json文件,而它返回我[Object,Object]。我出了什么问题?

最佳答案

解决方案

你就快到了!

只需将日志语句中的+更改为,即可。

像这样:

console.log("Server response ", response.data);

原因

+ 运算符将尝试将对象连接到字符串,该对象不是字符串,因此它调用 toString()函数,它向您显示[Object,Object],这意味着您有一个包含对象的对象。

当您使用逗号时,它将检查对象。

来自 toString()文档:

Every object has a toString() method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, the toString() method is inherited by every object descended from Object. If this method is not overridden in a custom object, toString() returns "[object type]", where type is the object type. The following code illustrates this:

关于javascript - 无法以 Angular 读取 json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35495115/

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