Thursday, June 5, 2014

Best Quotes by best people...

  1. It seems that perfection is attained not when there is nothing more to add, but when nothing more to subtract. - Antoine de Saint-Exupery
  2. "No problem can be solved from the same consciousness that created it." - Albert Einstein
  3. "The way to get started is to quit talking and begin doing." – Walt Disney
  4. It is always the simple that produces the marvelous.
  5. "Changing The Face" can change nothing. But "Facing The Change" can change everything.
  6. Don’t complain about others; Change yourself if you want peace.
  7. The journey of a thousand miles begins with one step.
  8. You cannot believe in God until you believe in yourself.
  9. In a day, when you don't come across any problems - you can be sure that you are travelling in a wrong path.
  10. Find a place inside where theres joy, and joy will burn out the pain.
  11. If you fail never give up because F.A.I.L. means "First Attempt In Learning".
  12. End is not the end, in fact E.N.D means "Effort Never Dies".
  13. If you get NO as an answer, remember N.O. means "Next Opportunity".
  14. Fortune favours the Brave.
  15. The most important thing is to enjoy your life - to be happy - it's all that matters.

Wednesday, June 4, 2014

Cross Site Scripting in WCF Web Service. How to use AJAX in JavaScript to Get/Consume JSON from WCF in C#


WCF (Windows Communication Foundation) is a latest service oriented architecture. You can expose methods in WCF depend on the requirement. For example if we want to expose method which will return JSON data. then we need to mention this type of contract in method declaration. In this article I am focusing on exposing method which return string (JSON) and how a client can call this method from JavaScript (AJAX call using jQuery)

To expose a method in WCF as JSON we need to provide valid Contract:

[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string GetJSONData(string param1);

Above method accept one parameter "param1". WCF will expose this method to out side world and it can be used in AJAX call. This method can be consumed directly using query string also. If service url is:
https://MyServiceURL/Service.svc/json/
then we can call this method as:
https://MyServiceURL/Service.svc/json/GetJSONData?param1=123

AJAX call can also be setup easily with dataType as JSONP in case you are seeting up in cross domain. Jsonp is json with padding specially for cross domain request, if you are hosting a client application in different domain compare to service then you need to use datatype as JSONP in ajax request.

var ajaxRequest = $.ajax({
        type: "GET",
        url: https://MyServiceURL/Service.svc/json/GetJSONData?callback?",
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",
        data: { param1: "123" },
        success: function (oJSONObject) {
    // Your code here
 }