A
A
Alexander Sobolev2021-12-08 22:48:49
AJAX
Alexander Sobolev, 2021-12-08 22:48:49

Bug with ajax file upload. What's wrong?

Hello!
Very strange behavior of the script bundle below. The bug is that "once again" the handler reports that the action is empty ..
and it either loads or not ..

jQuery(document).on('click', '#media-file-upload', function(event){ 
        
    event.stopPropagation(); event.preventDefault();  			
    if( typeof files == 'undefined' ) return( showResponse("error","Вы не выбрали файл") );			
    var data = new FormData(); var action = 'rotator_upload_media';
    data.append("action", action);
    $.each( files, function( key, value ){ data.append( key, value ); });			
    
    $.ajax({
      url         : ajaxurl,
      type        : 'POST',
      data        : data,
      cache       : false,
      dataType    : 'text',
      processData : false,
      contentType : false,
      success     : function( respond, status, jqXHR ){ 
        console.log(respond);
        //showResponse(status, "Загрузка успешно завершена");
        //$.each( JSON.parse(respond), function( key, value ){ console.log( key+": "+value ); });	
      },
      error: function( jqXHR, status, errorThrown ){	showResponse("error","ОШИБКА AJAX запроса: " + status +" " + jqXHR ); },
    });
  });


if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {

    if( isset($_POST['action']) ){

      switch ($_POST['action']) {
        case 'rotator_update_options':	echo json_encode(rotator_update_options($_POST['options'])); break;	
        case 'rotator_upload_media':	json_encode(rotator_upload_media($_FILES)); break;	
      }

    }
    
    elseif( isset($_GET['action']) || !empty($_GET['action']) ){
        
      switch ($_GET['action']) {
        case 'rotator_get_options'	 :  echo json_encode(rotator_get_options()); break;
      }
    }

    else { die("Ошибка выполнения: AСTION пуст или неверен!"); }

  }


unction rotator_upload_media($files){ 
    
    $uploaddir = '../medialib';
        
    if( ! is_dir( $uploaddir ) ) mkdir( $uploaddir, 0777 );

    foreach( $files as $file ){
        $file_name = cyrillic_translit( $file['name'] );

        if( move_uploaded_file( $file['tmp_name'], "$uploaddir/$file_name" ) ){            
            $path = "$uploaddir/$file_name";
            $ThisFileInfo['path'] = $path;
            echo json_encode($ThisFileInfo);
        }
            
    }    
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vitaly_74, 2021-12-09
@vitaly_74

maybe HTTP_X_REQUESTED_WITH is not always set on the server, dig in this direction, because if this if does not work, then nothing is displayed and, in a sense, ajax does not receive anything.
or else, ajax itself doesn't submit the form. but if it always sends, but sometimes does not receive a response, then it's in php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question