ajax request sends POST data but is not received

1

I have this code that sends data from a form to an script external

$(document).on("submit","#previewOc", function(e) {
                e.preventDefault();
                $.ajax({
                    url: "assets/contentHtml/ocs/preview.php",
                    type: "POST",
                    data:  new FormData(this),
                    contentType: false,
                    processData: false,
                    success: function(data){
                         VentanaCentrada(\'assets/contentHtml/ocs/preview.php\',\'Orden\',\'\',\'1024\',\'768\',\'true\');
                        console.log(data);

                    }
                });
            });

And in the console it appears that if the data POST was sent, but in my script , at the time of doing var_dump ($_POST) only one empty array appears, I mean the data was not received POST array()

What's wrong with my code ?, Thanks.

    
asked by Fernando Garcia 18.04.2018 в 17:29
source

2 answers

1

you could do something like this:

<html>
  <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
      $(function () {

        $('form').on('submit', function (e) {

          e.preventDefault();

          $.ajax({
            type: 'post',
            url: 'post.php',
            data: $('form').serialize(),
            success: function () {
              alert('form fue enviado');
            }
          });

        });

      });
    </script>
  </head>
  <body>
    <form>
      <input name="time" value="00:00:00.00"><br>
      <input name="date" value="0000-00-00"><br>
      <input name="submit" type="submit" value="Submit">
    </form>
  </body>
</html>

to access the data you could do it like this:

$_POST['nombreInput']

I hope I have helped you luck !!

    
answered by 18.04.2018 в 18:19
0

Good morning ... about your code try please make the following modifications:

var path = '../assets/contentHtml/ocs/preview.php?';

var valParam1 = $("#tuInput1").val();

var valParam2 = $("#tuInput2").val();

                $.ajax({    
                        type: "POST",  
                        url: path,
                        dataType: "html",
                        data: "param1="+valParam1+"&param2="+valParam2, 
                        success: function(data){                            
                        console.log(data);                      
                        },
                            error: function(XMLHttpRequest, textStatus, errorThrown) { 
                            console.log("err");
                        }       
                });

Now if you need to take all your form to the other side in your ajax would be with a varible like the following:

var tuForm = $("#idForm").serialize();

var valParam1 = tuForm;

and in your ajax you send it in the following way:

data: "formulario="+valParam1, 

In your "script.php" please check with the following code lines:

echo "<pre>";
print_r($_REQUEST);
echo "</pre>";

I hope it will be useful for you. Greetings.

    
answered by 18.04.2018 в 17:44