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

     });