jQuery/JSON/PHP failing

Posted by user730936 on Stack Overflow See other posts from Stack Overflow or by user730936
Published on 2012-06-10T10:28:56Z Indexed on 2012/06/10 10:40 UTC
Read the original article Hit count: 143

Filed under:
|
|

I am trying to call a php script that accepts JSON data, writes it into a file and returns simple text response using jQuery/AJAX call.

jQuery code :

   $("input.callphp").click(function() {
    var url_file = myurl";
    $.ajax({type : "POST",
            url : url_file,
            data : {'puzzle': 'Reset!'},
            success : function(data){
                alert("Success: " + data);
            }, 
            error : function (data) {
                alert("Error: " + data);
            },
            dataType : 'text'
    });
});

PHP Code :

<?php
  $thefile = "new.json"; /* Our filename as defined earlier */
  $towrite = $_POST["puzzle"]; /* What we'll write to the file */
  $openedfile = fopen($thefile, "w");
  fwrite($openedfile, $towrite);
  fclose($openedfile);
  echo "<br> <br>".$towrite;
?>

However, the call is never a success and always gives an error with an alert "Error : [Object object]".

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery