繁体   English   中英

Ionic 2中的.json

[英].json in Ionic 2

我在Ionic 2中的一个应用程序中工作,我正在将它与node.js服务器连接。 对于发送数据(服务器-Ionic),我这样发送:

http.createServer(function (req, res){
...
res.end(data);  // data is 0 or 1
}

在Ionic中,我得到这样的数据:

this.http.post("http://192.168.1.100:8080/post", 'PidoDatosClima' + '_' + this.parameter1)
            .subscribe(data => {
                resp=data.json()
                console.log(resp);
...

其中resp是0或1,因此...在此示例中可以正常工作。

我的问题是,当我需要在服务器中发送更多数据时,...如果在“ res.end(data)”中,数据是字符串“ 1_2_3”

在Ionic中,出现此错误:

例外:SyntaxError:JSON中位置1处的意外令牌_

有人知道我该怎么解决?

在您的服务器中尝试以下操作:

var data = { "value" : "1_2_3" };

res.end(JSON.stringify(data));  // Now data is an object with the 1_2_3 value

然后在离子代码中:

this.http.post("http://192.168.1.100:8080/post", 'PidoDatosClima' + '_' + this.parameter1)
         .map(res => res.json())
         .subscribe(data => {
                console.log(data.value);   // Access the value property
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM