JSON响应未定义:使用XMLHttpRequest [英] JSON response undefined: using XMLHttpRequest

查看:154
本文介绍了JSON响应未定义:使用XMLHttpRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XMLHTTPRequest来获取JSON响应。当我查看Firebug中的Response选项卡时,它向我显示JSON对象,我已经在 jsonlint 上进行了验证。当我尝试访问对象的属性时,我得到


TypeError:obj未定义


我已经研究了几个小时,但一直没能找到可行的解决方案。



代码:


$ b $

 函数getState(light){
var txt = execute('GET','http://'+ bridge +'/ api /'+ hash +'/ lights /'+ light);
var obj = eval('('+ txt +')');
// var obj = JSON.parse(txt);
//这给了我SyntaxError:JSON.parse:意外字符
console.log(obj.name);


函数execute($ method,$ url,$ message){//用于PUT和GET请求
var xmlhttp = new XMLHttpRequest();
xmlhttp.open($ method,$ url,true)
xmlhttp.send($ message);
console.log(xmlhttp.response);
return xmlhttp.response;
}

在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:扩展颜色灯,
name:左后客厅1,
modelid:LCT001,
swversion: 65003148,
pointsymbol:{
1:none,
2:none,
3:none,
4:none,
5:none,
6:none,
7:none,
8:none
}
}

我的getState函数(在页面加载中),console.log声明xmlhttp.response是一个空字符串。对'txt'和'obj'做typeof返回undefined。我尝试访问对象元素,例如:

obj.name obj.state.on



我是使用JSON和XMLHttpRequest的新手 - 我的代码基于别人最初创建的模板。我使用上述函数在我的程序的其他地方使用PUT请求没有问题,但似乎无法获得GET请求。



请让我知道如果我离开取出任何重要的信息。感谢您提供的任何帮助!

解决方案

您的XML HTTP请求被设置为异步(即脚本不会等待直到收到响应,并在后台发生HTTP请求时继续)

这意味着没有足够的时间存在 xmlhttp.response ,然后再返回。因此, txt 未定义,因此 obj 未定义,正如错误消息所述。



更改 xmlhttp.open 调用,以便调用是同步的(即脚本会一直等到HTTP响应在继续之前被接收):

  xmlhttp.open($ method,$ url,false); // true =>异步,false =>同步


I am using XMLHTTPRequest to get a JSON response. When I view the Response tab in Firebug, it shows me the JSON object, which I have validated on jsonlint. When I try to access the object's properties, I get

TypeError: obj is undefined

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

{
  "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. Doing typeof on 'txt' and 'obj' returns undefined. I'm trying to access the object elements, such as:

obj.name and obj.state.on

I am new to using JSON and XMLHttpRequest - my code is based on a template someone else had initially created. 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.

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 (i.e. the script doesn't wait until the response is received and continues while the HTTP request is happening in the background).

This means that there is not enough time for there to be an xmlhttp.response before you return it. As a consequence, txt is undefined so obj is undefined, just as the error message said.

Change the xmlhttp.open call so that the call is synchronous (i.e. the script waits until the HTTP response is received before continuing):

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

这篇关于JSON响应未定义:使用XMLHttpRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆