【问题标题】:.json in Ionic 2离子 2 中的 .json
【发布时间】:2017-01-19 16:34:45
【问题描述】:

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

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: 位置 1 处 JSON 中的意外标记 _

有人知道我该如何解决吗?

【问题讨论】:

  • 你能告诉我们你在运行你的邮政服务时得到了什么吗?这可能与返回数据的方式有关,但我不想假设。
  • 您没有收到 json,因此您可能不会使用 json()。也许使用 (data)._body (私有财产)
  • 感谢您的回复。拜托,你能举一个例子吗?我做了一些测试,但没有用。提前致谢!

标签: json angular typescript ionic2


【解决方案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
...

【讨论】:

    猜你喜欢
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 2017-12-28
    • 2016-12-27
    • 2018-04-23
    • 1970-01-01
    相关资源
    最近更新 更多