Jaime Rodriguez On Windows Phone, Windows Presentation Foundation, Silverlight and Windows 7
This will walk you through how to make REST (GET,POST) calls from a sidebar gadget using XmlHttp.. ( 20 mins or less, if you have asp .net) There are plenty of samples on the web for using XmlHttp, so will go quick and we will touch in a few of the 'more intricate' scenarios people have asked about: Cookies + Session State + redirects when sidebar gadgets call a back-end... The code for this sample is located here; there is a
Here is the gist of the Client-side code: loginFlyout.html Just has form data... trivial HTML form prompting for username & password for form user authentication. The "useGet" determines if making a POST or GET request, though practically they are the same code.
generic.js has the function to get the XmlHttp object, first we check for native IE 7 object, if not possible we use MSXML..
function
login.js has the code to send the XmlHttp request.. Note that for this samples we used Synchronous calls... that is usually bad :( ... but it saves mein sample code and keeps it simpler so I left as an exercise for you to change ..
function submitForm() { var item ; var DataToSend = "" ; var x ;
//parse the form to get the fields out ... and build the payload for ( x = 0 ; x < document.form1.elements.length ; x++ ) { DataToSend += document.form1.elements[x].id + "=" + document.form1.elements[x].value + "&" ; }
var xmlhttp = GetXMLHTTP() ;var useGET = document.getElementById('useGET').value; if ( useGET ) {xmlhttp.open("GET","http://jaimersvr/UsernameSite/Logon.aspx?ReturnUrl=%2fUsernamesite%2fSessionPage.aspx&" + DataToSend, false);// Note this is a Synchronous call.. you should not do this.. call it async.. I did it this way to keep code simple .. . xmlhttp.send();} else {xmlhttp.open("POST","http://jaimersvr/UsernameSite/Logon.aspx?ReturnUrl=%2fUsernamesite%2fSessionPage.aspx", false); xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");// Note this is a Synchronous call.. you should not do this.. call it async.. I did it this way to keep code simple .. .xmlhttp.send(DataToSend );}
// This is example for a flyout communicating with main document ... // check the System.Gadget.document documentation System.Gadget.document.getElementById ('result').innerHTML = xmlhttp.responseText;
}
That is all the client side code ... :) I will not explain the server side code unless I start getting requests for it.. I am guessing any one who is familiar with ASPX will know how to install it ... and those that are not familiar will be OK just knowing that REST works well from within sidebar..
Here is what the end to end sample does:
Here is how to run it:
Here are some of my observations:
Here are the gotchas or issues I saw (which for most part might not be an issue):
Hope that was useful; if the instructions are too hard ... let me know ..