HTTP(S) Request With Javascript and receive JSON data -
since i'm new on http(s) request case, have 1 case here must written on javascript, hope me because i've searched everywhere , couldn't find answer. here case:
however, when connection rate goes 5, have wait until 1 of them finished corresponding before sending request, 2 or more of them not on 5. in addition, when response code not 200, retry 3 times. if response code still not 200 after retrying 3 times, error function pursue. must receive json data of response body argument function.
<!doctype html> <html> <head> <script> function loadxmldoc() { var xmlhttp; if (window.xmlhttprequest) { xmlhttp=new xmlhttprequest(); } else { xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getelementbyid("mydiv").innerhtml=xmlhttp.responsetext; } else if (xmlhttp.readystate==4 && xmlhttp.status!=200){ document.write("error"); } } xmlhttp.open("get","demo_get.asp",true); xmlhttp.send(); } </script> </head> <body> <h2>ajax</h2> <button type="button" onclick="loadxmldoc()">request data</button> <div id="mydiv"></div> </body> </html>
please on comment below or can suggest me links me case. thanks.
something this?
concerning connection rate, can go multiple directions this, since isn't 100% clear want. easiest solution have asp page return 'fake error' json object , have success
function check if response contains 'fake error'. if so, resend request server. or have asp page send response, if connection rate < 5, might mean user ends waiting longer expected.
var getjson = function getjson( resource, success, failure ) { var xmlhttp = (window.xmlhttprequest) ? xmlhttp=new xmlhttprequest() : new activexobject("microsoft.xmlhttp"); xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate === 4 && xmlhttp.status === 200) { retry = 0; success(xmlhttp.responsetext, 200); } else failure(xmlhttp.status); } xmlhttp.open("get", resource, true); xmlhttp.send(); }, retry = 0, success = function success( response, status ) { document.queryselector("#mydiv").textcontent = response; }, failure = function failure( status ) { if (retry < 3) { retry += 1; getjson("demo_get.asp", success, failure); } else console.log('error'); }; document.queryselector('button').addeventlistener('click', function( event ) { getjson("demo_get.asp", success, failure); });
Comments
Post a Comment