简体   繁体   中英

Can't receive data, ajax, javascript, jquery, php

I am trying to send data that i got from a jquery call and now i want to save it to a file on the server with php.

function getSVG(){
   svghead = svghead + $('#test').html();

   $.ajax({
            type:"POST",
            data: svghead,
            url: "xyz.php",
             success:function(data){
                 .....
            }
        });
    }

and in php my code is:

<?php
   header('Content-Type: text/html; charset=utf-8'); 

   $data = $_POST['data'];

   $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
   fwrite($myfile, $data);
   fclose($myfile);
?>

but the created file never contains any value. it s just empty. as far as i tested, i guess it is just a syntax mistake but where or what is wrong? thx for any help

You need to specify the parameter name in order to PHP recognize it:

data: {
    data: svghead
}

With the object above you're sending a data paramenter with the value equal to your svghead variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM