javascript - JSON.parse(hr.response) error -



javascript - JSON.parse(hr.response) error -

i trying retrieve info json. have written code. alerts "1" doesn't alert "2".

<script type="text/javascript" src="jquery-migrate-1.2.1.min.js"></script> <script type="text/javascript"> function ajax_get() { var results = document.getelementbyi("results"); var hr = new xmlhttprequest(); hr.open("get", "mylist.json", true); hr.responsetype = "json"; hr.setrequestheader("content-type", "application/json",true); hr.onreadystatechange = function() { if (hr.readystate == 4 && hr.status == 200) { alert('1'); var info = json.parse(hr.response); alert('2'); alert(data); results.innerhtml = data.name; } } hr.send(null); results.innerhtml = "request ..."; } </script> </head> <body> <div id="results"></div> <script type="text/javascript">ajax_get();</script> </body>

you have set response type json on line.

hr.responsetype= "json";

so need not parse response again. default json. create sure response in json format , alter code this.

var info = hr.response;

javascript jquery json

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -