CS174
Chris Pollett
Nov 18, 2020
request = new XMLHttpRequest();
request.setRequestHeader("name", "value");
request.open(theHTTPmethod, theURL, theAsync flag) or request.open(theHTTPmethod, theURL, theAsync flag, username, password)
request.open("GET", "test-news.html", true); request.onreadystatechange = function() { switch(request.readyState) { case 0:// handle uninitialized case case 1: // handle open but no send case case 2: // handle send but no response case case 3: // handle response is being downloaded case case 4: // handle response has completed being downloaded case } }
document.getElementById("myDivTag").innerHTML = request.responseText; //or we could do... myDiv = document.getElementById("myDivTag") if(myDiv.firstChild) { myDiv.removeChild(myDiv.firstChild); } myDiv.appendChild(document.createTextNode(request.responseText));
request = new XMLHttpRequest(); request.onreadystatechange = function() { switch(request.readyState) { case 4: document.getElementById("test-news").innerHTML = request.responseText; break; } } request.open("GET", "test_news.html", true); request.send();
request.send(null);Note: send's argument can be used if using POST method to send the posted data
my_object = JSON.parse(request.responseText);
http://www.yioop.com/s/news?f=rss&limit=20&num=30
url/my_obj/method1?arg_1_for_method1=value_1& arg_2_for_method1=value_2... url/my_obj/method2?arg_1_for_method2=value_1& arg_2_for_method2=value_2... ...
12.3 -- an example Number "hi there" -- an example String true -- an example Boolean, other possibility false
[1, 4, 9]
{"bob": 29, "sally" : 35}
myObj = eval( "(" + data + ")" );
<?php define ('YIOOP_NEWS_URL', 'https://www.yioop.com/s/news/'); // look at incoming request fields $query = (!empty($_REQUEST['f']) && in_array($_REQUEST['f'], ["rss", "json"])) ? "?f=" . $_REQUEST['f'] : ""; $url = YIOOP_NEWS_URL . $query; // Open the Curl session $request_handle = curl_init($url); // If we wanted to use post we could uncomment the lines below //curl_setopt ($request_handle, CURLOPT_POST, true); //curl_setopt ($request_handle, CURLOPT_POSTFIELDS, $postvars); // Don't bother with the HTTP headers. Do bother with the content back curl_setopt($request_handle, CURLOPT_HEADER, false); curl_setopt($request_handle, CURLOPT_RETURNTRANSFER, true); // Make proxy request $response_content = curl_exec($request_handle); //determine the content-type of the respeonse $pre_content_type = curl_getinfo($request_handle, CURLINFO_CONTENT_TYPE); $content_type = (empty($pre_content_type)) ? "text/html" : $pre_content_type; // Set the outgoing proxy content type header("Content-Type: $content_type"); echo $response_content; curl_close($request_handle);