简体   繁体   English

无法接收数据,ajax,javascript,jquery,php

[英]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. 我正在尝试发送从jquery调用中获取的数据,现在我想将其保存到使用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中,我的代码是:

<?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: 您需要指定参数名称才能使PHP识别它:

data: {
    data: svghead
}

With the object above you're sending a data paramenter with the value equal to your svghead variable. 使用上面的对象,您将发送一个值等于svghead变量的data参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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