2012-04-05 36 views
1

我遇到问题了。我尝试向Web服务器XBMC发送JSON请求。我可以在Wireshark中看到POST请求被正确发送,并且响应由Web服务器发送,但在Javascript中,我无法使用JSON数据在警报中显示它。XMLHTTPREQUEST在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); 
+0

你为什么用'application/x-www-form-urlencoded'内容类型发送编码为'application/json'的数据? – Quentin 2012-04-05 10:30:25

+0

会发生什么?你有空的警报吗?浏览器是否在其JavaScript控制台中显示任何错误消息? – Quentin 2012-04-05 10:32:22

+0

我收到一个空警报。我发送一个应用程序/ x-www-form-urlencoded,因为如果我使用application/json发送它,xbmc不理解JSON命令 – 2012-04-05 10:53:25

回答