Problem using $ .ajax with safari

0

I have the following situation, I am using a $. ajax POST to send data to the server but I have a detail, when I load the site in a Safari browser either in a Mac or in a Ipad the process does not run to me and it keeps loading in:

 beforeSend: function(){
                                message = $("<div class='alert alert-danger'><span class='before'>Subiendo, por favor espere...</span></div>");
                                showMessage(message)        
                            }

Searching in internet I found a similar problem, in which they only had to add the following lines:

                        async: true,
                        headers: {
                            "cache-control": "no-cache"
                          },

I added them but the problem is not solved. Then I leave the code to see if you can help me.

  

Note: It does not generate any errors in the development console of    Safari .

Thanks.

                    $.ajax({
                        type: "POST",
                        url: "index.php",
                        data: dataString,
                        cache: false,
                        contentType: false,
                        processData: false,
                        async: true,
                        headers: {
                            "cache-control": "no-cache"
                          },                                
                        //mientras enviamos el archivo
                        beforeSend: function(){
                            message = $("<div class='alert alert-danger'><span class='before'>Subiendo, por favor espere...</span></div>");
                            showMessage(message)        
                        },                                
                        success: function (datos) {
                            if(!id){
                                $("input#idnombre").val("");
                                $("textarea#idatalle").val("");
                                $('select#grupo').prop('selectedIndex',-1);
                                $('select#id_usuario').prop('selectedIndex',-1);
                            }
                            //$("textarea#idatalle").val("");
                            $("div#iderror").html(datos);
                        },
                        //si ha ocurrido un error
                        error: function(){
                            message = $('<div class="alert alert-danger">Ha ocurrido un error.</div>');
                            $( "#accion" ).prop( "disabled", false );
                            showMessage(message);
                        }                                
                    });
    
asked by Yoel Rodriguez 30.07.2018 в 23:13
source

1 answer

1

I leave the solution to this problem I had with safari in the $. ajax .

What happens is that safati , you do not allow empty elements when you are using input file in the form. The field was going empty, so I was having the $. Ajax function stuck in beforeSend causing a timeaout in the execution.

The solution that I represent is for the case that you are using FormData to capture the data of the form.

Thank you.

var dataString = new FormData($(this)[0]);

if (document.getElementById("id elemento input file").files.length == 0 ) {
    dataString.delete("nombre del elemento input file"); 
}
    
answered by 01.08.2018 в 19:45