Friday, April 25, 2014

Call a method after multiple AJAX requests are finished through jQuery

You may face a situation where you need to make multiple AJAX requests and call your function after all those requests are completed.

jQuery provides a way to handle this neatly:


$.when($.ajax( "/page1" ), $.ajax( "/page2" ) ).then(function( a1, a2)
{
    // a1 and a2 are arguments resolved for the page1 and page2 ajax requests
    // Each argument is an array with the following structure: [ data, statusText,jqXHR ]
});

Friday, April 18, 2014

CSS media queries for different Internet Explorer versions

/* IE6/7 uses media, */
@media, { 
.container { color: green; } 
.item { color: red; }
} 

/* IE8 uses \0 */
@media all\0 { 
.container { color: brown; }
.item { color: orange; }} 

/* IE9 uses \9 */
@media all and (monochrome:0) { 
.container { color: yellow\9; }           .
.item { color: blue\9; }} 

/* IE10 and IE11 both use -ms-high-contrast */
@media all and (-ms-high-contrast:none)
 {
 .foo { color: green } /* IE10 */
 *::-ms-backdrop, .foo { color: red } /* IE11 */
 }

Tuesday, April 8, 2014

JSONP calls in ASP.NET MVC

JSONP is "JSON with padding".  It is used to access or transfer data across the domains.  The code below is used to access JSONP data from a remote server and fetch the results in ASP.NET MVC

public ContentResult FetchData()
        {
            string signedUrl = ""; //URL to fetch jsonp data from
            WebRequest request = (HttpWebRequest)WebRequest.Create(signedUrl);
            request.Method = WebRequestMethods.Http.Get;
            string result = string.Empty;

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    result = reader.ReadToEnd();
                }
            }

            return Content(result, "application/javascript");
        }