1

我有问题。我尝试向 Web 服务器 XBMC 发送 JSON 请求。我可以在 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);
4

1 回答 1

1

我会建议你使用一些 javascript 框架,例如 jQuery。看看http://api.jquery.com/jQuery.getJSON/http://api.jquery.com/jQuery.ajax/

如果您使用 jQuery 的 ajax 函数,您将不需要编写那么多的 javascript。

于 2012-04-05T10:57:38.413 回答