Saturday, 21 December 2013

How to make CORS request in Javascript and Jquery.

Here I included a basic tutorial for making CORS(Corss Origin Resource Sharing) request from Javascript and Jquery.
1.Include Jquery library on your page.
2.See below code which is fully functional for CORS request.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.js"></script>
<script>
// Create the XHR object.
function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    // XHR for Chrome/Firefox/Opera/Safari.
    xhr.open(method, url, true);
  } else if (typeof XDomainRequest != "undefined") {
    // XDomainRequest for IE.
    xhr = new XDomainRequest();
    xhr.open(method, url);
  } else {
    // CORS not supported.
    xhr = null;
  }
  return xhr;
}

// Helper method to parse the title tag from the response.
function getTitle(text) {
  return text.match('<title>(.*)?</title>')[1];
}

// Make the actual CORS request.
function makeCorsRequest() {
  // All HTML5 Rocks properties support CORS.
  var url = 'http://updates.html5rocks.com';

  var xhr = createCORSRequest('GET', url);
  if (!xhr) {
    alert('CORS not supported');
    return;
  }

  // Response handlers.
  xhr.onload = function() {
    var text = xhr.responseText;
    var title = getTitle(text);
    alert('Response from CORS request to ' + url + ': ' + title);
  };

  xhr.onerror = function() {
    alert('Woops, there was an error making the request.');
  };

  xhr.send();
}
function jqueryCorsRequest(){
$.ajax({

  // The 'type' property sets the HTTP method.
  // A value of 'PUT' or 'DELETE' will trigger a preflight request.
  type: 'GET',

  // The URL to make the request to.
  url: 'http://updates.html5rocks.com',

  // The 'contentType' property sets the 'Content-Type' header.
  // The JQuery default for this property is
  // 'application/x-www-form-urlencoded; charset=UTF-8', which does not trigger
  // a preflight. If you set this value to anything other than
  // application/x-www-form-urlencoded, multipart/form-data, or text/plain,
  // you will trigger a preflight request.
  contentType: 'text/plain',

  xhrFields: {
    // The 'xhrFields' property sets additional fields on the XMLHttpRequest.
    // This can be used to set the 'withCredentials' property.
    // Set the value to 'true' if you'd like to pass cookies to the server.
    // If this is enabled, your server must respond with the header
    // 'Access-Control-Allow-Credentials: true'.
    withCredentials: false
  },

  headers: {
    // Set any custom headers here.
    // If you set any non-simple headers, your server must include these
    // headers in the 'Access-Control-Allow-Headers' response header.
  },

  success: function(data) {
    // Here's where you handle a successful response.
alert("Success: "+data);
  },

  error: function(error) {
    // Here's where you handle an error response.
    // Note that if the error was due to a CORS issue,
    // this function will still fire, but there won't be any additional
    // information about the error.
alert("Error: "+error);
  }
});
}
</script>
</head>

<body>
<input name="" type="button" value="Create Cors Request" onclick="makeCorsRequest();" /><br />
<input name="" type="button" value="Jquery Cors Request" onclick="jqueryCorsRequest();"/>
</body>
</html>

Monday, 9 December 2013

How to change pages in Jquery Mobile.

Here I included complete tutorial for how to changing pages in Jquery Mobile that runs on all mobile platform.

Steps to creating this is as follow
1.Include Jquery library in your html page
2.Add data-role and id property in your div tag which you want to make page define more div in your html page for adding more pages in your app.
3.Full source is as bellow.

<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css">
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script>
  function loadPage2() {
    /* show wait page */
    $.mobile.loading( 'show', {
            text: 'Loading',
            textVisible: true
    });
 
     /*  fake AJAX call */  
     setTimeout(callback, 1500);
 }

function callback() {
    $.mobile.changePage("#page2");
}
function loadPage3() {
    /* show wait page */
    $.mobile.loading( 'show', {
            text: 'Loading',
            textVisible: true
    });
 
     /*  fake AJAX call */  
     setTimeout(callback1, 1500);
 }

function callback1() {
    $.mobile.changePage("#page2");
}
</script>
</head>
<body>
<div id="page1" data-role="page">
    <div data-role="header">
        <h1>Page 1</h1>
    </div>
    <div data-role="content">
        <input type='Text'>
        <p>Go to <a href="#" onclick='loadPage2();'>page 2</a>.</p>
        <p>Go to <a href="#" onclick='loadPage3();'>page 3</a>.</p>
    </div>
</div>

<div id="page2" data-role="page">
    <div data-role="header">
        <h1>Page 2</h1>
    </div>
    <div data-role="content">
        <p>Go back to <a href="#page1">page 1</a>.</p>
        <p>Go to <a href="#page3">Page 3</a></p>
    </div>
</div>
<div id="page3" data-role="page">
    <div data-role="header">
        <h1>Page 3</h1>
    </div>
    <div data-role="content">
        <p>Go back to <a href="#page1">page 1</a>.</p>
        <p>Go to <a href="#page2">Page 2</a></p>
    </div>
</div>
</body>
</html>