Friday, January 26, 2018

Share data between html pages

  1. Pop out a window using a hashtag and appends the data that needs to be shared
    • var childNoteWindow = window.open(URL + "popout.html#" + JSON.stringify(params), target, windowoption);
  2. The pop out window retrieves the data
    • var paramString =window.location.hash.substr(1);
  3. The pop out window clears the hashtag detail from the URL. Added bonus: this does not cause page refresh.
    • history.pushState("", document.title, window.location.pathname + window.location.search);
Only available with HTML5.

Tuesday, January 2, 2018

JQuery call to WCF service

Using jQuery to communicate to an Ajax-enabled WCF Service

WCF Service Definition using object response
[OperationContract]
[WebInvoke(Method = "POST")]
public TestResponse TestService(string input)
{
    try
    {
        TestResponse response = new TestResponse() { Message = string.Format("TestService result is {0}", input) };

        return response;
    }
}

JQuery Service Call using object
       $.ajax({
             type: "POST",
              url: "http://localhost:5274/MyService.svc/TestService",
             data: '{"input":"16"}',
              contentType: "application/json; charset=utf-8",
              success: function (data) {
                    var tmp = data.d;
                    alert(‘Message: ‘ + tmp.Message);
              },
              error: ServiceCallFailed
     });

---------------------------------------------------------------------------------------------


WCF Service Definition using string response
[OperationContract]
[WebInvoke(Method = "POST")]
public string TestService(string input)
{
    try
    {
        TestResponse response = new TestResponse() { Message = string.Format("TestService result is {0}", input) };

        JavaScriptSerializer js = new JavaScriptSerializer(); //using System.Web.Script.Serialization;
        string json = js.Serialize(response);

        return json;
    }
}

JQuery Service Call using parse
       $.ajax({
             type: "POST",
              url: "http://localhost:5274/MyService.svc/TestService",
             data: '{"input":"16"}',
              contentType: "application/json; charset=utf-8",
              success: function (data) {
                    var response = $.parseJSON(data.d);
                    alert('Message: ' + response.Message);          },
              error: ServiceCallFailed

     });

Friday, December 29, 2017

WCF Service and Corss-Origin Resource Sharing (CORS)

1.) Add a Global.asax file to the WCF service
2.) Add the following into the "Application_Begin Request" Method
           
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
    HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
    HttpContext.Current.Response.End();
}

Wednesday, December 27, 2017

Include SVG definition in CSS



//CSS
.bold {
background-image: url('data:image/svg+xml;charset=UTF-8,');
}

//HTML

Thursday, December 14, 2017

jQuery AJAX call

$.ajax({
                async: true,
                dataType: 'xml',
                url: URL,
                success: function (data) {
                    div.innerHTML = new XMLSerializer().serializeToString(data.documentElement);
                },
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("" + errorThrown);
}
            });

Tuesday, August 2, 2016

Quick and easy way to consume a REST service


//Define URI
string uri = http://blah/blah/

//Define REST variable
string id = "someId";

//Create WebClient and consume REST service
string response = null;
using(WebClient client = new WebClient())
{
    response = client.UploadString(string.Format("{0}{1}", uri, id), "");
}

Friday, June 10, 2016

Dynamically stream image to webform

Dynamically stream image to Panel control in WebForm

1.) Convert Stream to byte[] buffer
byte[] buffer = new byte[pd.Stream.Length];
pd.Stream.Read(buffer, 0, buffer.Length);


2.) Dynamically create Image control and set the ImageURL to the buffer
System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
img.ImageUrl = "data:image/tiff;base64," + Convert.ToBase64String(content, 0, content.Length);

 3.) Add Image control to the Panel
 DisplayPanel.Controls.Add(img);