Skip to content

Commit

Permalink
added test with simple ajax enabling cors on the server that doesn't …
Browse files Browse the repository at this point in the history
…need jsonp issue #1
  • Loading branch information
ppazos committed Jun 24, 2015
1 parent eb75d08 commit 1ac6760
Showing 1 changed file with 168 additions and 8 deletions.
176 changes: 168 additions & 8 deletions index.htm
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,71 @@
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
// https://gist.github.com/malsup/82181
// fn to handle jsonp with timeouts and errors
// hat tip to Ricardo Tomasi for the timeout logic
$.getJSONP = function(s) {
s.dataType = 'jsonp';
$.ajax(s);

// figure out what the callback fn is
var $script = $(document.getElementsByTagName('head')[0].firstChild);
var url = $script.attr('src') || '';
var cb = (url.match(/callback=(\w+)/)||[])[1];
if (!cb)
return; // bail
var t = 0, cbFn = window[cb];

$script[0].onerror = function(e) {
$script.remove();
handleError(s, {}, "error", e);
clearTimeout(t);
};

if (!s.timeout)
return;

window[cb] = function(json) {
clearTimeout(t);
cbFn(json);
cbFn = null;
};

t = setTimeout(function() {
$script.remove();
handleError(s, {}, "timeout");
if (cbFn)
window[cb] = function(){};
}, s.timeout);

function handleError(s, o, msg, e) {
// support jquery versions before and after 1.4.3
($.ajax.handleError || $.handleError)(s, o, msg, e);
}
};

$.handleError = function(s, xhr, status, e) {
// If a local callback was specified, fire it
if ( s.error ) {
s.error.call( s.context || window, xhr, status, e );
}

// Fire the global callback
if ( s.global ) {
(s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] );
}
};

var ehr_server = {

base_url: undefined,
get_patients: "/rest/patientList?format=json&callback=?",
get_patient: "/rest/getPatient?uid=${uid}&format=json&callback=?",
get_ehrs: "/rest/ehrList?format=json&callback=?",
get_ehr: "/rest/ehrGet?ehrUid=${uid}&format=json&callback=?",
//get_ehr_for_patient: "/rest/ehrForSubject?subjectUid=${uid}&format=json&callback=?",
get_ehr_for_patient: "/rest/ehrForSubject",
get_queries: "/rest/queryList?format=json&callback=?",

init: function(url) {
this.base_url = url;
Expand All @@ -19,8 +77,8 @@
callback(data);
});
},
patient: function (uid, callback) {
$.getJSON(this.base_url + this.get_patient, null, function(data) {
patient: function (patient_uid, callback) {
$.getJSON(this.base_url + this.get_patient.replace("${uid}", patient_uid), null, function(data) {
console.log( data );
callback(data);
});
Expand All @@ -30,14 +88,52 @@
console.log( data );
callback(data);
});
},
ehr: function (ehr_uid, callback) {
$.getJSON(this.base_url + this.get_ehr.replace("${uid}", ehr_uid), null, function(data) {
console.log( data );
callback(data);
});
},
ehr_for_patient: function (patient_uid, callback) {
/*
$.getJSONP(this.base_url + this.get_ehr_for_patient.replace("${uid}", patient_uid), null, function(data) {
console.log( data );
callback(data);
})
*/

$.ajax({
url: this.base_url + this.get_ehr_for_patient,
method: 'GET',
data: {subjectUid: patient_uid, format: 'json'}
})
.done( function (data) {
callback(data);
})
.fail( function (jqXHR, textStatus, errorThrown) {
data = $.parseJSON(jqXHR.responseText); // for errors 400/500 ajax doesnt do json parsin
//console.log(parsedjson, textStatus, errorThrown);
callback(data); // TODO: allow passing an error callback
});
},
queries: function (callback) {
$.getJSON(this.base_url + this.get_queries, null, function(data) {
console.log( data );
callback(data);
})
.fail(
function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
}
);
}
};


$(document).ready(function() {

console.log(ehr_server);

ehr_server.init('http://localhost:8090/ehr');

$('#patients').on('click', function (){
Expand All @@ -48,13 +144,48 @@
);
});

$('#patient').on('click', function (){
ehr_server.patient(
$('input[name=patient_uid]').val(),
function(data) {
$('#out').text( JSON.stringify(data, null, " ") )
}
);
});

$('#ehrs').on('click', function (){
ehr_server.ehrs(
function(data) {
$('#out').text( JSON.stringify(data, null, " ") )
}
);
});

$('#ehr').on('click', function (){
ehr_server.ehr(
$('input[name=ehr_uid]').val(),
function(data) {
$('#out').text( JSON.stringify(data, null, " ") )
}
);
});

$('#ehr_for_patient').on('click', function (){
ehr_server.ehr_for_patient(
$('input[name=ehr_patient_uid]').val(),
function(data) {
$('#out').text( JSON.stringify(data, null, " ") )
}
);
});

$('#queries').on('click', function (){
ehr_server.queries(
function(data) {
$('#out').text( JSON.stringify(data, null, " ") )
}
);
});


//var url = 'http://localhost:8090/ehr/rest/patientList?format=json&callback=?';
Expand All @@ -64,10 +195,39 @@

});
</script>
<style>
fieldset {
display: inline;
margin: 1em;
border: 1px solid #aaa;
}
textarea {
width: 100%;
}
</style>
</head>
<body>
<button id="patients">patients</button>
<button id="ehrs">ehrs</button>
<textarea id="out" rows="20" cols="80"></textarea>
<div id="buttons">
<button id="patients">patients</button>
<fieldset>
<input type="text" name="patient_uid" placeholder="patient uid" />
<button id="patient">patient</button>
</fieldset>

<button id="ehrs">ehrs</button>
<fieldset>
<input type="text" name="ehr_uid" placeholder="ehr uid" />
<button id="ehr">ehr</button>
</fieldset>
<fieldset>
<input type="text" name="ehr_patient_uid" placeholder="patient uid" />
<button id="ehr_for_patient">ehr for patient</button>
</fieldset>

<button id="queries">queries</button>
</div>
<div id="output">
<textarea id="out" rows="40"></textarea>
</div>
</body>
</html>
</html>

0 comments on commit 1ac6760

Please sign in to comment.