0

I'm backend developer and pretty new in Javascript. I'm trying to upload files with old JQuery version (1.4.2), because there is no way to change it on current project.

This code works good on ANY JQuery version >= 1.5

$('#uploadform').submit(function(e) {
    var formData = new FormData(this);
    $.ajax({
       type:'POST',
       url: '/uploader',
       data:formData,
       xhr: function() {
           var myXhr = $.ajaxSettings.xhr();
           if(myXhr.upload){
               //
           }
           return myXhr;
       },
       cache:false,
       contentType: false,
       processData: false,

       success: function(data){
           //
       },

       error: function(data){
           //
       }
   });
});

Each time a run it on JQuery 1.4.2, I have this error in Chrome (and other browsers): POST http://localhost:8080/uploader net::ERR_CONNECTION_RESET in jquery-1.4.2.js:5252

Is there a way to fix it without JQuery version change?

UPD

Here is my <head> section in html

<head>
<meta charset="UTF-8">
<title>Remedy Uploader</title>
<link type="text/css" rel="stylesheet" href="style.css" media="screen"/>
<script type='text/javascript' src="https://code.jquery.com/jquery-1.4.2.js"></script>
<script type='text/javascript' src="script.js"></script>

silent-box
  • 1,649
  • 3
  • 21
  • 40

2 Answers2

0
var oldXHR = jQuery.ajaxSettings.xhr;
jQuery.ajaxSettings.xhr = function() {
    var xhr = oldXHR();
    if(xhr instanceof window.XMLHttpRequest) {
        xhr.upload.addEventListener('progress', on_progress, false);
        xhr.upload.addEventListener('load', on_loaded, false);
        xhr.addEventListener('abort', on_abort, false);
    }
    return xhr;
};

$.ajax({
    xhr: function() {
        var xhr = jQuery.ajaxSettings.xhr();
        if(xhr instanceof window.XMLHttpRequest) {
            xhr.upload.addEventListener('progress', on_progress, false);
            xhr.upload.addEventListener('load', on_loaded, false);
            xhr.addEventListener('abort', on_abort, false);
        }
        return xhr;
    }
});

check below URL

How to get pure XMLHTTPRequest object in jQuery 1.5?

Community
  • 1
  • 1
Nik Varma
  • 158
  • 9
0

First you should try to comment all code at http://localhost:8080/uploader and just print a hello message. Then test it in browser, It must be working. If it does not work the fix it first, but when works then try following request as it is

with jquery 1.4.2 you can use same ajax request as it is used today

$.ajax({
    type: "POST",
    //I commented followin gintentionally, you should check the 
    // before un-commenting this line
    //data : formData code 
    url: 'http://localhost:8080/uploader',
    success: function(data){alert(data)},
    error: function(err){ alert(er.responseText)};
});

Off course you will face no error

Sami
  • 8,168
  • 9
  • 66
  • 99
  • I didn't quite get what you mean, but I ran this code (relplace formData code with `new FormData(this);` Result is `jquery-1.4.2.js:5437 Uncaught TypeError: Illegal invocation` – silent-box Aug 05 '16 at 11:09
  • You should get no error if you run it as it is. Problem could be with your `new FormData()` Yes i can guide about this problem, If exactly it is `FormData` then till me, i will help – Sami Aug 05 '16 at 13:09