[英]XMLHTTPREQUEST response with JSON in Javascript?
I've a problem.我有问题。 I try to send a JSON request to a web server XBMC.我尝试向 web 服务器 XBMC 发送 JSON 请求。 I can see in Wireshark the POST Request is sent correctly and the response is sent by the web server but, in Javascript, I can't take the JSON Data to show it in a alert.我可以在 Wireshark 中看到 POST 请求已正确发送,响应由 web 服务器发送,但是在 Javascript 中,我无法将 JSON 数据显示在警报中。
var xhr_object = null;
if(window.XMLHttpRequest) // Firefox
xhr_object = new XMLHttpRequest();
else if(window.ActiveXObject) // Internet Explorer
xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
else { // XMLHttpRequest non supporté par le navigateur
alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
return;
}
xhr_object.open("POST", "http://"+add+":9000/jsonrpc", false);
xhr_object.onreadystatechange = function() {
if(xhr_object.readyState == 4)
var json = xhr_object.responseText;
alert(xhr_object.responseType)
alert("("+json+")");
}
xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var data = '{"jsonrpc": "2.0", "method": "Input.Up", "id": "1"}';
xhr_object.send(data);
I'll suggest you to use some javascript framework eg jQuery. Have a look at http://api.jquery.com/jQuery.getJSON/ and http://api.jquery.com/jQuery.ajax/ .我建议您使用一些 javascript 框架,例如 jQuery。看看http://api.jquery.com/jQuery.getJSON/和http://api.88419763389.ax.aj.com/jQuery/jQuery/jQuery .
You won't need to write that much of javascript if you use jQuery's ajax function.如果你使用 jQuery 的 ajax function,你就不需要写那么多 javascript。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.