Javascript XMLHttpRequestJSON.parse()
对编码来说还是个新鲜事物 我需要做一个XMLHttpRequest,然后使用JSON.parse来检索一个对象。一旦我有了对象,就需要根据对象的“类型”将对象显示为大标题、标题或段落 不使用JSON.parse:Javascript XMLHttpRequestJSON.parse(),javascript,json,xml,for-loop,xmlhttprequest,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()
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET", "http://****/chapters/?n=0",false);
xmlhttp.send();
result=xmlhttp.responseText;
document.getElementById("sec1").innerHTML=result;
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;
我得到以下信息
{"data": " PART ONE ", "type": "bigheading"}
使用JSON.parse:
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET", "http://****/chapters/?n=0",false);
xmlhttp.send();
result=xmlhttp.responseText;
document.getElementById("sec1").innerHTML=result;
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的末尾实现某种类型的FOR循环(n=0-38),因为这表示节数。我一次只能显示一个部分。不确定如何在URL中实现FOR循环。谢谢大家! 像这样的东西怎么样:
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
为什么不能有一个单独的请求将所有38个值作为一个数组返回呢?实际上,只需要显示数组的前5个部分/值,然后每隔5个部分/值显示一个。只是给我的标准。但是我不知道如何在URL链接中使用计数器/循环