XMLHttpRequest JSON.parse()

时间:2015-05-29 02:40:10

标签: javascript json xml for-loop xmlhttprequest

仍然很安静新编码

我需要创建一个XMLHttpRequest,然后使用JSON.parse来检索一个对象。一旦我有了对象需要根据它的"类型"显示对象。作为大头,标题或段落。

不使用JSON.parse:

             var xmlhttp=new XMLHttpRequest();

            xmlhttp.open("GET", "http://****/chapters/?n=0",false);

            xmlhttp.send();

            result=xmlhttp.responseText;

            document.getElementById("sec1").innerHTML=result;

我得到以下

           {"data": " PART ONE ", "type": "bigheading"}

使用JSON.parse:

               var xmlhttp=new XMLHttpRequest();

            xmlhttp.open("GET", "http://xxxxxx/chapters/?n=0",false);

            xmlhttp.send();

            result=xmlhttp.responseText;

            JSONresult = JSON.parse(result);

            document.getElementById("sec1").innerHTML=JSONresult;

结果是:

      [object Object]

还需要为URL的末尾(n = 0-38)实现某种类型的FOR LOOP,因为这表示节的数量。我一次只能显示一个部分。不确定如何在URL中实现FOR循环。谢谢!

1 个答案:

答案 0 :(得分:0)

这样的事情怎么样:

var section = document.getElementById("sec1");
section.className += ' ' + JSONResult.type;    // Style the section by the type
section.innerHTML = JSONResult.data;           // Fill in the section with the data