Last active
October 19, 2015 10:43
-
-
Save jpalala/c81700e197c3b9eb33fc to your computer and use it in GitHub Desktop.
Cors simple sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createCORSRequest(method, url) { | |
var xhr = new XMLHttpRequest(); | |
if ("withCredentials" in xhr) { | |
// XHR for Chrome/Firefox/Opera/Safari. | |
xhr.open(method, url, true); | |
console.log('withCredentials') } else if (typeof XDomainRequest != "undefined") { | |
// XDomainRequest for IE. | |
xhr = new XDomainRequest(); | |
xhr.open(method, url); | |
} else { | |
// CORS not supported. | |
xhr = null; | |
} | |
return xhr; | |
} | |
function makeCorsRequest() { | |
var xhr = createCORSRequest('GET', url); | |
if (!xhr) { | |
alert('CORS not supported'); | |
return; | |
} | |
xhr.onload = function() { | |
console.log("onloadn"); | |
} | |
xhr.onerror = function() { | |
alert('Error accessing database.'); | |
}; | |
xhr.send(); | |
} | |
$(document).ready(function() { | |
// are we running in native app or in a browser? | |
window.isphone = false; | |
if(document.URL.indexOf("http://") === -1 | |
&& document.URL.indexOf("https://") === -1) { | |
window.isphone = true; | |
} | |
if( window.isphone ) { | |
document.addEventListener("deviceready", onDeviceReady, false); | |
} else { | |
onDeviceReady(); | |
} | |
}); | |
function showMentors() { | |
// if (str=="") { | |
//if (str=="") | |
// { | |
//document.getElementById("txtHint").innerHTML = ""; | |
//return; | |
// } | |
if (window.XMLHttpRequest) | |
{ | |
// code for IE7+, Firefox, Chrome, Opera, Safari | |
xmlhttp=new XMLHttpRequest(); | |
} | |
else | |
{ // code for IE6, IE5 | |
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); | |
} | |
xmlhttp.onreadystatechange=function() | |
{ | |
if (xmlhttp.readyState==4 && xmlhttp.status==200) | |
{ | |
//you can use JSON.parse(jsonobject_xmlresponseText) to ensure it is parsed as an json array. for example: . | |
mentorList = JSON.parse(xmlhttp.responseText); | |
var options = ''; | |
for(i=0; i <= mentorList.length - 1 ; i++) | |
{ | |
var mentorId = mentorList[i].userid; | |
console.log(mentorId); | |
mentorName = mentorList[i].fullname; | |
console.log(mentorName); | |
options += '<option value="' + mentorId + '">' + mentorName + '</option>'; | |
console.log(options); | |
} | |
//document.getElementById("mentors_helptxt").innerHTML = xmlhttp.responseText; | |
} | |
} | |
// Pass the select option to PHP page | |
xmlhttp.open("GET","http://www.mentorsdojo.com/mentors/?json",true); | |
xmlhttp.send(); | |
} | |
//sample plain string parse and put into innerHTML | |
function showUser(str) | |
{ | |
if (str=="") | |
{ | |
document.getElementById("txtHint").innerHTML = ""; | |
return; | |
} | |
if (window.XMLHttpRequest) | |
{ // code for IE7+, Firefox, Chrome, Opera, Safari | |
xmlhttp=new XMLHttpRequest(); | |
} | |
else | |
{ // code for IE6, IE5 | |
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); | |
} | |
xmlhttp.onreadystatechange=function() | |
{ | |
if (xmlhttp.readyState==4 && xmlhttp.status==200) | |
{ | |
document.getElementById("txtHint").innerHTML = xmlhttp.responseText; | |
} | |
} | |
// Pass the select option to PHP page | |
xmlhttp.open("GET","http://www.myDomain.com/getcars.php?q="+str,true); | |
xmlhttp.send(); | |
} | |
// Start the CORS on startup | |
var url = "http://www.mentorsdojo.com/"; | |
var method = "GET"; | |
createCORSRequest(method, url); | |
showMentors(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment