2016-11-01 18 views
0

私はこの言語スタックを使い慣れていません...サーバー上の.jsonファイルにajax投稿のデータが埋め込まれていない理由を理解しようとしています。以下は2つの機能です。最初はブラウザで動作するjquery関数で、もう1つはjqueryによって呼び出されるPHP関数です。サーバ上のディレクトリのファイルアクセス権は、/ var/wwwで読み書き可能ですjQuery AJAXサーバーファイルにJSONデータを書き込まない投稿

現在のところ、php関数がjsonデータを書き込む必要がある、長さゼロのファイルpost_Sched.jsonがあります。

<script> 
    function theFunction() { 
        console.log("about to post to post_Sched.php") 
     console.log("post_Sched_jstringed = " + post_Sched_jstringed); 

     request = $.ajax({ 
      url: "post_Sched.php", 
      type: "POST", 
      data: { 'data': post_Sched_jstringed }, 
      //dataType: "JSON", 
      success: function (response) { 
       // response from php 
       $("#jresult").text(JSON.stringify(response)); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) { 
       console.log(textStatus, errorThrown); 
      } 
     }); 
     console.log("just posted to post_Sched.php") 
    } 
</script> 
</head> 
<body> 
     <h2>JSON Post Response: </h2> 
     <div id="jresult"></div> 
</body> 
</html> 

<?php 
    $data = $_POST['data']; 
    $file = fopen('post_Sched.json','w'); 
    fwrite($file, $data); 
    fclose($file); 
    echo json_encode($data); 
?> 

P.S. post_Sched.phpと呼ばれるPHPファイルそこJSONデータのためのjQueryの関数内のグローバル変数であり、私は適切なstringify'ed結果を見ることができますので、私はjsonDatが正しいことを知っている:

var post_Sched_jstringed = JSON.stringify(Sched); 

開発ツール・コンソールはparsererrorのにSyntaxErrorを示しています。予期しないトークン< 0番のJSONで

誰でも問題を説明し、解決する方法はありますか?
ありがとうございます!

+0

から

data: {data: jsonDat}, 

data: jsonDat, 

を交換してうまくいけば、あなたのPHPコードでDOM準備ハンドラとconsole.logsを持っていませんか? – adeneo

+0

はい、明らかに私はしました。私は現在の状態を表示するために、ここにPHPを編集しました。私はまた、ここに表示されているものを実行し、まだ動作していません。 – Walt

+0

"ところで、私はObjectTextText(chromeの開発者ツール)のPHP関数全体を見ます。"というように、PHP関数をHTMLのように返します。私はPHPを知らないのですが、それを読んでコードを実行していないと、が表示されない、またはそれがどうなっているのですか、その意図ですか? –

答えて

0

http://api.jquery.com/jquery.ajax/

data

Type: PlainObject or String or Array

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

<?php 

//echo 'Current PHP version: ' . phpinfo(); 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    $json = $_POST['post_Sched_jstringed']; 
    print_r($_POST); 

    $file = fopen('post_Sched.json','w+'); 
    fwrite($file, json_encode($json)); // The object is automatically deserialized, so if you want it as json, it has to be serialized again 
    fclose($file); 

} 

?> 
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> 
<script> 
    function theFunction() { 
     console.log("about to post to post_Sched.php") 

     request = $.ajax({ 
      url: "", // Take note the url was changed to same page, while I was working with this. 
      //dataType: "json", // Removing this resolved the client-side error 
      type: "post", 
      data: { // I strongly recommend having data be an object with key-value pairs. 
       post_Sched_jstringed: { 
        prop1: 232, 
        prop2: "asfasf" 
       } 
      }, 

      success: function (response) { 
       // response from php 
       console.log(response); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) { 
       console.log(textStatus, errorThrown); 
      } 
     }); 
     console.log("just posted to post_Sched.php") 
    } 
</script> 
+0

残念ながら、それはそれをしませんでした。私は、PHP以外のスクリプト言語(おそらくPython)以外の別のサーバー側スクリプト言語を使用するつもりだと思います。私は開発段階の早い段階にあり、より身近なものになるでしょう。 – Walt

+0

問題の現在の状態を取り入れるために質問にいくつかの更新を行いました。現在の問題の状態:JSONデータがpost_Sched.jsonファイルに追加されず、POSTからの応答でパーサエラーが報告されています – Walt

+0

PHPをインストールして少し実験しましたが、 "json"データをキーと値のオブジェクトに変更しました。ファイルに何も保存していなかったのは、オブジェクトが変数にシリアル化されていないことです。シリアル化されていますが、それはjsonとして私は再びそれを直列化しました。この例のコメントは変更を示しています。 –

関連する問題