Created
May 11, 2015 06:24
-
-
Save kn9ts/eb27ca44602c63bee6dc to your computer and use it in GitHub Desktop.
Submiting a form using ajax, the somehow long way
This file contains 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
// get the form | |
var theForm = document.getElementById("vitumob-user-form"); | |
var formdata = new FormData(theForm); | |
//Add the HTML file for the site that failed to be filtered | |
formdata.append("part_html", status.html); | |
// Though: why not also some important data | |
formdata.append("related_url", document.URL); | |
formdata.append("related_host", document.location.host) | |
formdata.append("error", status.reason); | |
// get the date | |
formdata.append("date", date.today()); | |
formdata.append("time", date.timeNow()); | |
formdata.append("browser_name", $.browser.name); | |
formdata.append("browser_version", $.browser.version); | |
formdata.append("OS_System", $.os.name); | |
// console.log(formdata); | |
$.ajax({ | |
url: URI, | |
type: "POST", | |
xhr: function() { // Custom XMLHttpRequest | |
var xhr = $.ajaxSettings.xhr(); | |
if (xhr.upload) { // Check if upload property exists | |
xhr.upload.addEventListener('progress', function(e) { | |
if (e.lengthComputable) { | |
//Show the uploading progress | |
setTimeout(function() { | |
if ((e.loaded / e.total) === 1) {} else {} | |
// console.log("uploaded -- " + ((e.loaded / e.total) * 100) + "%"); | |
}, 10); | |
// console.log("Total data size -- " + (e.total / 1024) + " Kbs"); | |
} | |
console.log("Total data size -- " + (e.total / 1024) + " Kbs"); | |
//Erase all form data for refresh uploading | |
formdata = undefined; | |
}, false); // For handling the progress of the upload | |
} | |
return xhr; | |
}, | |
data: formdata, | |
cache: false, | |
// contentType: false, | |
crossDomain: true, | |
processData: false, | |
dataType: "text", | |
beforeSend: function(xhr) { | |
/* | |
* Prevent this error below because of CORS: | |
* @error -- XMLHttpRequest cannot load http://djotjog.com/c/saveaudio. | |
No 'Access-Control-Allow-Origin' header is present on the requested resource. | |
Origin 'http://localhost:2323' is therefore not allowed access. | |
*/ | |
xhr.setRequestHeader("Access-Control-Allow-Origin", "true"); | |
} | |
}).done(function(response) { | |
//do something | |
if (response && response.success) { | |
alert(response.message); | |
} | |
}).fail(function(error) { | |
alert("An error occured -- " + JSON.stringify(error)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment