简体   繁体   中英

Call php script with ajax

I developped a web application (html,php5,javascript,ajax). I was using WAMPServer on windows and it was working right. I wanted to try it on ubuntu. I've installed LAMP and I put my project in /var/www/html after giving permissions I could run it on my browser. but sending data using ajax to a php script is not working and I don't know what is the problem. Can anyone help? even this simple example is not working :

 <html> 
   <head> 
     <script src="/assets/jquery1.11.2.js"></script> 
   </head>
   <body> 
      <button onclick="duplicate()"/>
      <script>
        function duplicate() 
              {
                var action = "CreationBoard"; 
                var res = $.ajax({
                    type : "POST",
                    url  : "file.php",
                    data : { action : action },
                });
            res.done(function(){
                alert("success :");
            });
            res.fail(function(jqXHR , textStatus){
                alert("faild"+textStatus);
            }); 
              } 
    </script> 
</body> 

and this is my file.php :

<?php 
   $res = $_POST['action']; 
   file_put_contents("result.txt",$res); 
?>

When the button is pressed a file named result.txt should be created, but it's not but I get a success Alert... I saw many posts about this, this is how it should be done (I think), please tell me what's wrong .. i'm not used to work with Ubuntu. This example is working on Windows

install firebug and check what the response is.

what sort of permissions do you have on /var/www/html ? Might be that you have not given it the "proper" permissions and php is not allowed to write into that specific directory.

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