Thursday, March 31, 2016

CROS Calls using javascript jQuery Ajax

This content is form answer of the question how-does-access-control-allow-origin-header-work
from stackoverflow. I am just keeping one copy of that answer at my blog for future reference :

Access-Control-Allow-Origin is a CORS (Cross-Origin Resource Sharing) header.
When Site A tries to fetch content from Site B, Site B can send an Access-Control-Allow-Originresponse header to tell the browser that the content of this page is accessible to certain origins. (Anorigin is a domain, plus a scheme and port number.) By default, Site B's pages are not accessible to any other origin; using the Access-Control-Allow-Origin header opens a door for cross-origin access by specific requesting origins.
For each resource/page that Site B wants to make accessible to Site A, Site B should serve its pages with the response header:
Modern browsers will not block cross-domain requests outright. If Site A requests a page from Site B, the browser will actually fetch the requested page on the network level and check if the response headers list Site A as a permitted requester domain. If Site B has not indicated that Site A is allowed to access this page, the browser will trigger the XMLHttpRequest's error event and deny the response data to the requesting JavaScript code.

Non-simple requests

What happens on the network level can be slightly more complex than explained above. If the request is a "non-simple" request, the browser first sends a data-less "preflight" OPTIONS request, to verify that the server will accept the request. A request is non-simple when either (or both):
  • using an HTTP verb other than GET or POST (e.g. PUT, DELETE)
  • using non-simple request headers; the only simple requests headers are:
    • Accept
    • Accept-Language
    • Content-Language
    • Content-Type (this is only simple when its value is application/x-www-form-urlencoded, multipart/form-data, or text/plain)
If the server responds to the OPTIONS preflight with appropriate response headers (Access-Control-Allow-Headers for non-simple headers, Access-Control-Allow-Methods for non-simple verbs) that match the non-simple verb and/or non-simple headers, then the browser sends the actual request.
Supposing that Site A wants to send a PUT request for /somePage, with a non-simple Content-Typevalue of application/json, the browser would first send a preflight request:
OPTIONS /somePage HTTP/1.1
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: Content-Type
Note that Access-Control-Request-Method and Access-Control-Request-Headers are added by the browser automatically; you do not need to add them. This OPTIONS preflight gets the successful response headers:
Access-Control-Allow-Methods: GET, POST, PUT
Access-Control-Allow-Headers: Content-Type
When sending the actual request (after preflight is done), the behavior is identical to how a simple request is handled. In other words, a non-simple request whose preflight is successful is treated the same as a simple request (i.e., the server must still send Access-Control-Allow-Origin again for the actual response).
The browsers sends the actual request:
PUT /somePage HTTP/1.1
Content-Type: application/json

{ "myRequestContent": "JSON is so great" }
And the server sends back an Access-Control-Allow-Origin, just as it would for a simple request:
See Understanding XMLHttpRequest over CORS for a little more information about non-simple requests.
Happy Living...
Happy Coding...

Thursday, August 27, 2015

Search Text in schema of Stored Procedure

With a big database this is one of the basic issue if you want to search whether a particular string/value available in anywhere in schema of stored procedure.
So we cant do it manually (yeah we have option of looking into dependencies but this is one step ahead) like It will help us to search in even comments too.

FROM sys.Procedures
WHERE object_definition(OBJECT_ID) LIKE '%This part is commented%'

Happy Living...
Happy Coding...
Happy Concepts ...

Thursday, June 11, 2015

Reset or Normalize default User Code Stylesheet of Browser

Here i am going to talk about "User Code Style sheet". It is default style sheet of browser get applied on Dom elements if nothing is specified about how to handle elements appearance on page.


But it might get applied if the tags are not closed properly and will lead to different UI display.

Solution :

To resolve this issue we have 2 either to reset it or normalize stylesheet.

Reset CSS: (Get Reset Css)
Resets browsers defauts

Normalize CSS: 
Preserve useful browser defaults rather than erasing them. 
Normalize styles for a wide range of HTML elements.
Correct bugs and common browser inconsistencies. 
Improve usability with subtle improvements. 
Explain the code using comments and detailed documentation.

Note: Try to put it after all your style sheet to implement inherit property. 

Happy Living.....
Happy Concepts.....
Happy Coding.....

Friday, May 22, 2015

Enable Nuget packages in Visual Studio 2010

The NuGet is easier way to include dlls or other projects/utilities in your projects. If we aren't using it , in one way we are selecting some tough way to achieve something that can be implemented in straight forward way.

Open Visual studio in menu bar click on Tools-> Extension Manager

fig 1

It will open a new popup as shown below, In this popup you can see there isn't any Extension Manager installed. Once we install Nuget it will appear in this screen.

fig 2
Now we will do following steps:
1. Click on Online Gallery under Installed Extensions.
2. Type Nuget in search text box at right top corner
3. Now it will appear in middle area of window now click to Download.

fig 3
Now you will get another popup for installation read term and conditions and click on install.

fig 4

On completion of installation there will be a message box, close it.

fig 5

Once it get completed, Restart Visual Studio  you will able to see it under Installed Extensions.

fig 6

And its done. In next blog we will check how to use NuGet Package Manager.

Happy Living....
Happy Coding....
Happy Concepts....

Wednesday, November 5, 2014

Change Doc type to IE8 or IE8 Compatibility mode

Change Doc type to IE8 or IE8 Compatibility mode:

There are some old development that doesn't support in newer version of IE and Chrome. So best option is to make changes in code work for latest IE versions and Chrome. But this is the case not always possible for example previous code was built using third party controls (Infragistics in my case) to make it work with Newer version of IE we need upgraded version on Infragistics and it involves addition cost of licensing.
One other work around if it is possible to support only IE  browser than we can use solution suggested below: (BUT it is not best solution)

 <title> My Page</title>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8">

<meta http-equiv="X-UA-Compatible" content="IE=8">

Note : Make sure to use meta tag just after title tag otherwise it wont work.

Happy Coding...
Happy Concepts...
Happy Living....

Monday, May 5, 2014

Convert local datetime to UTC or Universal data time and UTC to local datetime

--Steps required before performing conversion:


Declare @TimezoneOffset int=0

-- variable will hold the timezone difference in minutes

set @TimezoneOffset =  DATEDIFF(Minute, GETUTCDATE(), GETDATE())

--Now we have localtimezone difference with UTC.

--Convert Local datetime to UTC:-

Declare @LocalDatetime datetime= GetDate()

Declare @UTCDatetime datetime =null

select  @UTCDatetime= DateAdd(Minute,-@TimezoneOffset,@LocalDatetime)

--Convert UTC to Local datetime :-

 Declare @ConvertedLocalDatetime datetime=null

select @ConvertedLocalDatetime= DateAdd(Minute, (@TimezoneOffset),@UTCDatetime)

select 'LocalTime:-',@LocalDatetime,'  UTC Datetime : ', @UTCDatetime ,'ConvertedLocalDatetime : ', @ConvertedLocalDatetime

Monday, April 7, 2014

Download file using javascript/Jquery

On aspx page add this:

<iframe id="hiddenDownloaderForPdf" style="display:none" ></iframe>

on Javascript file:

function downloadFile (url) {
        var hiddenDownloaderForPdf= 'hiddenDownloaderForPdf'
        iframe = document.getElementById(hiddenDownloaderForPdf);
//if iframe isnt available create one dynamiclly
    if (iframe === null) {
        iframe = document.createElement('iframe'); = hiddenDownloaderForPdf; = 'none';
    iframe.src = url;

using jQuery:
function downloadFile (url) {
        var hiddenIFrameID = 'hiddenDownloaderForPdf'

       $('#'+ iframe).attr('src' ,url);

Happy Living.....
Happy Concepts.......
Happy Coding........