简体   繁体   English

Ionic 2中的.json

[英].json in Ionic 2

Im working in one application in Ionic 2 and I'm connecting it with node.js server. 我在Ionic 2中的一个应用程序中工作,我正在将它与node.js服务器连接。 For send data (server - Ionic) I send like this: 对于发送数据(服务器-Ionic),我这样发送:

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

In Ionic, I get the data like this: 在Ionic中,我得到这样的数据:

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

Where resp is 0 or 1 so... in this example work fine. 其中resp是0或1,因此...在此示例中可以正常工作。

My problem is when I need to send more data in my server so... if in "res.end(data)" data is the string "1_2_3" 我的问题是,当我需要在服务器中发送更多数据时,...如果在“ res.end(data)”中,数据是字符串“ 1_2_3”

In Ionic, I get this error: 在Ionic中,出现此错误:

EXCEPTION: SyntaxError: Unexpected token _ in JSON at position 1 例外:SyntaxError:JSON中位置1处的意外令牌_

Somebody know how can I solve it? 有人知道我该怎么解决?

Try with something like this in your server: 在您的服务器中尝试以下操作:

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

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

And then in Ionic code: 然后在离子代码中:

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