javascript - Get data out of JSON array from external URL -
i relatively new json. have read tutorial , trying implement no luck. have external url gives json data/feed. data in form of array. trying write javascript program (on local) data out of url , put in html.
here function. includes external link also. not getting result. empty. missing or doing wrong?
<!doctype html> <html> <head> <meta charset="utf-8"> <title>index page</title> </head> <body> <div id="id01"></div> <script> var xmlhttp = new xmlhttprequest(); var url = "http://mpatrizio-001-site5.smarterasp.net/categorylist.php?d=b7acef70-4901-41c8-930f-d4d681d82daa"; xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { var myarr = json.parse(xmlhttp.responsetext); myfunction(myarr); } } xmlhttp.open("get", url, true); xmlhttp.send(); function myfunction(arr) { var out = ""; var i; for(i = 0; < arr.length; i++) { out += arr[i].categoryid + '<br>'; } document.getelementbyid("id01").innerhtml = out; } </script> </body> </html>
update:
after being pointed in right direction guys (thank that), have found request being blocked server due cors error. studying it. please review following image of error got in console. it, can point out solution?
append --disable-web-security
(at path c;...\chrome.exe) in chrome's exe properties preceded space.
more elegant solution:
other solution on server side. create crossdomain.xml
, clientaccesspolicy.xml
file on server. it's structure like:
crossdomain.xml:
<!doctype cross-domain-policy system "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> <cross-domain-policy> <allow-http-request-headers-from domain="*" headers="soapaction,content-type"/> </cross-domain-policy>
clientaccesspolicy.xml:
<?xml version="1.0" encoding="utf-8"?> <access-policy> <cross-domain-access> <policy> <allow-from http-request-headers="soapaction"> <domain uri="*"/> </allow-from> <grant-to> <resource path="/" include-subpaths="true"/> </grant-to> </policy> </cross-domain-access> </access-policy>
some of tutorials are:
http://help.adobe.com/en_us/as2lcr/flash_10.0/help.html?content=00000469.html
http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html
its specification is:
http://www.adobe.com/devnet-docs/acrobatetk/tools/appsec/crossdomain_policyfile_specification.pdf
other tutorials:
https://msdn.microsoft.com/en-us/library/cc197955(v=vs.95).aspx
Comments
Post a Comment