简体   繁体   English

JSON响应未定义:使用XMLHttpRequest

[英]JSON response undefined: using XMLHttpRequest

I am using XMLHTTPRequest to get a JSON response. 我正在使用XMLHTTPRequest来获取JSON响应。 When I view the Response tab in Firebug, it shows me the JSON object, which I have validated on jsonlint . 当我在Firebug中查看Response选项卡时,它会向我显示JSON对象,我已在jsonlint上验证了该对象。 When I try to access the object's properties, I get 当我尝试访问对象的属性时,我明白了

TypeError: obj is undefined TypeError:obj未定义

I have researched for several hours, but have not been able to find a working solution. 我研究了几个小时,但一直未能找到有效的解决方案。

Code: 码:

function getState(light) {
  var txt = execute('GET', 'http://' + bridge + '/api/' + hash + '/lights/' + light);
  var obj = eval('(' + txt + ')');
  //var obj = JSON.parse(txt);
  //this gives me "SyntaxError: JSON.parse: unexpected character"
  console.log(obj.name);
}

function execute($method, $url, $message) { //used for both PUT and GET requests
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open($method, $url, true)
  xmlhttp.send($message);
  console.log(xmlhttp.response);
  return xmlhttp.response;
}

In the Firebug Response tab, it indicates response of GET request: 200 OK -4ms 在Firebug Response选项卡中,它表示GET请求的响应:200 OK -4ms

{
  "state": {
    "on": false,
    "bri": 200,
    "hue": 8664,
    "sat": 140,
    "xy": [0.4932, 0.3832],
    "ct": 428,
    "alert": "none",
    "effect": "none",
    "colormode": "hs",
    "reachable": true
  },
  "type": "Extended color light",
  "name": "Left rear living room 1",
  "modelid": "LCT001",
  "swversion": "65003148",
  "pointsymbol": {
    "1": "none",
    "2": "none",
    "3": "none",
    "4": "none",
    "5": "none",
    "6": "none",
    "7": "none",
    "8": "none"
  }
}

When I call my getState function (on pageload), the console.log claims that xmlhttp.response is an empty string. 当我调用我的getState函数(在pageload上)时,console.log声称xmlhttp.response是一个空字符串。 Doing typeof on 'txt' and 'obj' returns undefined. 在'txt'和'obj'上执行typeof会返回undefined。 I'm trying to access the object elements, such as: 我正在尝试访问对象元素,例如:

obj.name and obj.state.on obj.nameobj.state.on

I am new to using JSON and XMLHttpRequest - my code is based on a template someone else had initially created. 我是使用JSON和XMLHttpRequest的新手 - 我的代码基于其他人最初创建的模板。 I have no problem with PUT requests I used elsewhere in my program, using the above functions, but cannot seem to get GET requests working. 我使用上面的函数在我的程序中的其他地方使用的PUT请求没有问题,但似乎无法让GET请求正常工作。

Please let me know if I left out any important info. 如果我遗漏了任何重要信息,请告诉我。 Thanks for any help you can provide! 感谢您的任何帮助,您可以提供!

Your XML HTTP request is set to be asynchronous (ie the script doesn't wait until the response is received and continues while the HTTP request is happening in the background). 您的XML HTTP请求设置为异步(即,脚本不会等到收到响应,并在HTTP请求在后台发生时继续)。

This means that there is not enough time for there to be an xmlhttp.response before you return it. 这意味着在返回之前没有足够的时间来存在xmlhttp.response As a consequence, txt is undefined so obj is undefined, just as the error message said. 因此, txt未定义,因此obj未定义,正如错误消息所述。

Change the xmlhttp.open call so that the call is synchronous (ie the script waits until the HTTP response is received before continuing): 更改xmlhttp.open调用,以便调用是同步的(即脚本等待,直到收到HTTP响应,然后继续):

xmlhttp.open($method, $url, false); // true => asynchronous, false => synchronous

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

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