Mercurial Hosting > freedit
annotate 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 |
| rev | line source |
|---|---|
| 7 | 1 |
| 2 function ajax(url,postData) { | |
| 3 let request = new XMLHttpRequest(); | |
| 4 let method = postData ? 'POST' : 'GET'; | |
| 5 request.open( method, url ); | |
|
33
4fdc4ec0050b
upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
6 let isMultipart = postData instanceof File; |
|
4fdc4ec0050b
upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
7 if( postData && !isMultipart ) |
| 7 | 8 request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); |
| 9 request.onload = function() { | |
| 10 if( request.status !== 200 ) { | |
| 11 window.console && console.log( 'ajax failed: ' + request.status ); | |
| 12 if( request.responseText ) | |
| 13 document.write('<pre>'+request.responseText+'</pre>'); | |
| 14 return; | |
| 15 } | |
| 16 eval( request.responseText ); | |
| 17 }; | |
|
33
4fdc4ec0050b
upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
18 if(isMultipart) { |
|
4fdc4ec0050b
upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
19 let formData = new FormData(); |
|
4fdc4ec0050b
upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
20 formData.append( 'file', postData ); |
|
4fdc4ec0050b
upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
21 request.send(formData); |
|
4fdc4ec0050b
upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
22 } else { |
|
4fdc4ec0050b
upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
23 request.send(postData); |
|
4fdc4ec0050b
upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
24 } |
| 7 | 25 } |
| 26 | |
| 27 window.onerror = function(msg, url, line, col, error) { | |
| 28 if (!url) | |
| 29 return; | |
| 30 let err = msg; | |
| 31 err += '\nurl = ' + url; | |
| 32 if (url != window.location) | |
| 33 err += '\npage = ' + window.location; | |
| 34 err += '\nline = '+line; | |
| 35 if (col) | |
| 36 err += '\ncolumn = ' + col; | |
| 37 if (error && error.stack) | |
| 38 err += '\nstack = ' + error.stack; | |
| 39 ajax( '/error_log.js', 'err='+encodeURIComponent(err) ); | |
| 40 }; |
