comparison src/site.js @ 33:4fdc4ec0050b

upload with uploadcare
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 01 Aug 2022 22:50:16 -0600
parents 33731231093a
children c8d47981c74f
comparison
equal deleted inserted replaced
32:72a1b77b4548 33:4fdc4ec0050b
1 1
2 function ajax(url,postData) { 2 function ajax(url,postData) {
3 let request = new XMLHttpRequest(); 3 let request = new XMLHttpRequest();
4 let method = postData ? 'POST' : 'GET'; 4 let method = postData ? 'POST' : 'GET';
5 request.open( method, url ); 5 request.open( method, url );
6 if( postData ) 6 let isMultipart = postData instanceof File;
7 if( postData && !isMultipart )
7 request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); 8 request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
8 request.onload = function() { 9 request.onload = function() {
9 if( request.status !== 200 ) { 10 if( request.status !== 200 ) {
10 window.console && console.log( 'ajax failed: ' + request.status ); 11 window.console && console.log( 'ajax failed: ' + request.status );
11 if( request.responseText ) 12 if( request.responseText )
12 document.write('<pre>'+request.responseText+'</pre>'); 13 document.write('<pre>'+request.responseText+'</pre>');
13 return; 14 return;
14 } 15 }
15 eval( request.responseText ); 16 eval( request.responseText );
16 }; 17 };
17 request.send(postData); 18 if(isMultipart) {
19 let formData = new FormData();
20 formData.append( 'file', postData );
21 request.send(formData);
22 } else {
23 request.send(postData);
24 }
18 } 25 }
19 26
20 window.onerror = function(msg, url, line, col, error) { 27 window.onerror = function(msg, url, line, col, error) {
21 if (!url) 28 if (!url)
22 return; 29 return;