Jsonp requests

by alkos333

HTML

<div id="message">
    <p></p>
</div>
<button>Show message</button>

CSS

#message {
    width: 10em;
    padding: 1em;
    border: 1px solid #000;
    text-align: center;
}

JavaScript

// Default AJAX settings
$.ajaxSetup({
    jsonp: 'jsonp', // add "&jsonp=?" to our URLs
    dataType: 'jsonp' // expect jsonp type on return
});

// Storage key variable for the database
var storeKey;

// Store some random message in JSON-P DB
$.get(
    'http://jsonpdb.appspot.com/add?obj={"msg":"Hello World"}',
    function(data){
        storeKey = data;
    }   
);

$('button').click(function(){
    $.get('http://jsonpdb.appspot.com/get?obj=' + storeKey,
          function(data){
              $('#message > p').text(data.msg);
          });
});