Origin is not allowed by Access-Control-Allow-Origin

I wrote an article on this issue a while back, Cross Domain AJAX. The easiest way to handle this if you have control of the responding server is to add a response header for: Access-Control-Allow-Origin: * This will allow cross-domain Ajax. In PHP, you’ll want to modify the response like so: <?php header(‘Access-Control-Allow-Origin: *’); ?> … Read more

“Origin null is not allowed by Access-Control-Allow-Origin” error for request made by application running from a file:// URL

For the record, as far as I can tell, you had two problems: You weren’t passing a “jsonp” type specifier to your $.get, so it was using an ordinary XMLHttpRequest. However, your browser supported CORS (Cross-Origin Resource Sharing) to allow cross-domain XMLHttpRequest if the server OKed it. That’s where the Access-Control-Allow-Origin header came in. I … Read more

How to read a local text file?

You need to check for status 0 (as when loading files locally with XMLHttpRequest, you don’t get a status returned because it’s not from a Webserver) function readTextFile(file) { var rawFile = new XMLHttpRequest(); rawFile.open(“GET”, file, false); rawFile.onreadystatechange = function () { if(rawFile.readyState === 4) { if(rawFile.status === 200 || rawFile.status == 0) { var … Read more

Send POST data using XMLHttpRequest

The code below demonstrates on how to do this. var http = new XMLHttpRequest(); var url=”get_data.php”; var params=”orem=ipsum&name=binny”; http.open(‘POST’, url, true); //Send the proper header information along with the request http.setRequestHeader(‘Content-type’, ‘application/x-www-form-urlencoded’); http.onreadystatechange = function() {//Call a function when the state changes. if(http.readyState == 4 && http.status == 200) { alert(http.responseText); } } http.send(params); In … Read more