JQuery.post - $ _POST is empty

I know that ajax and $ _POST calls have been around a lot lately, however I could not find an answer to my current problem.

In my Javascript, I have a two-dimensional data array:

var postData = new Array(new Array()); postData[0]['type'] = 'grid'; postData[0]['data'] = gridData; 

Then I try to send this array to a PHP script:

 function export_report_pdf(postData){ console.log(postData); $.post('/ajax/ExportReportPDF.ajax.php',{data: JSON.stringify(postData)}, function(postData){ console.log("Successfully requested report export."); }); } 

I tried to get an array in my PHP script: print_r ($ _ POST); var_dump (json_decode (file_get_contents ("PHP: // input")));

but all I get in my $ _POST is an empty two-dimensional array. When I run console.log (postData) at the beginning of my function, there is data.

I also checked $ _REQUEST and tried to remove JSON.stringify.

+4
source share
4 answers

Your internal variable type must be an object instead of an array, otherwise it will not be serialized correctly:

 var postData = []; postData.push({ type: 'grid', data: gridData }); 
+5
source

You tried to use get instead of post. try, which at least ensures that the data is transferred from the client to the server, and the problem is only related to the POST request.

Also, when you try to execute a GET, than check the console if you get any error.

0
source

Do not JSON.stringify your message data. jQuery will do it for you, regardless of whether you did it yourself, so it ends up with double coding. If you check your logs, you will see that after unencoding the data, PHP has one POST parameter that contains all your JSON encoded data.

Your message may look like this:

 $.post('/ajax/ExportReportPDF.ajax.php', {data: postData}, ... 
0
source
 function export_report_pdf(postData){ console.log(postData); $.ajax(url:'/ajax/ExportReportPDF.ajax.php',type:'POST',{data: JSON.stringify(postData)}, success:function(postData){ console.log("Successfully requested report export."); }); } 

try it. and make sure you have the latest jquery enabled.

-1
source

Source: https://habr.com/ru/post/1478987/


All Articles