我试图从XMLHTTPRequest中解析一个值,想知道是否有人可以帮助我。
当我console.log(getplayers)时,我的API调用工作正常。
但是我只希望控制台打印播放器的响应/“名称”。即“ simon”。
谢谢大家,非常感谢您的帮助/建议。
我试图放入console.log(getplayers.response.name);但它返回“未定义”。
我的JAVASCRIPT代码:
var getplayers = new XMLHttpRequest();
var getplayerdata = {
"jsonrpc": "2.0",
"id": 0,
"method": "call",
"params": {
"instance_id": "test-instance",
"zome": "main",
"function": "get_players",
"args": { }
}
};
getplayers.open("POST", 'http://localhost:8888', true);
getplayers.setRequestHeader('Content-type', 'application/json');
getplayers.send(JSON.stringify(getplayerdata));
console.log(getplayers);
我的控制台中的当前响应:
XMLHttpRequest {onreadystatechange: null, readyState: 1, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
onabort: null
onerror: null
onload: null
onloadend: null
onloadstart: null
onprogress: null
onreadystatechange: null
ontimeout: null
readyState: 4
response: "{"jsonrpc":"2.0","result":"{\"Ok\":[{\"player\":\"HcScjN8wBwrn3tuyg89aab3a69xsIgdzmX5P9537BqQZ5A7TEZu7qCY4Xzzjhma\",\"name\":\"simon\",\"balance\":1,\"reputation\":1}]}","id":0}↵"
responseText: "{"jsonrpc":"2.0","result":"{\"Ok\":[{\"player\":\"HcScjN8wBwrn3tuyg89aab3a69xsIgdzmX5P9537BqQZ5A7TEZu7qCY4Xzzjhma\",\"name\":\"simon\",\"balance\":1,\"reputation\":1}]}","id":0}↵"
responseType: ""
responseURL: "http://localhost:8888/"
responseXML: null
status: 200
statusText: "OK"
timeout: 0
upload: XMLHttpRequestUpload {onloadstart: null, onprogress: null, onabort: null, onerror: null, onload: null, …}
withCredentials: false
__proto__: XMLHttpRequest
在我的控制台中想要/预期的响应将仅打印响应/玩家/名称值:
'西蒙'