2

現在、xml を使用するアプリを作成しています。これを使用してxmlファイルにアクセスします-

$.ajax({
    type: "GET",
    url: "data/data.xml",
    dataType: "xml",
    success: function (xml) {
        data = xml;
        init(); 
    }
});

次に、 $(data).find などを使用してデータ内のいくつかの要素を変更すると、値が変更されるようです。

次に、この変数「データ」を変更してサーバーにアップロードする必要があります。私はそれを送り返すためのこのコードを持っています -

$.ajax({ 
    url: "saveXml.php", 
    type: "POST", 
    contentType: "text/xml", 
    processData: true, 
    data: data,
    success: function(){
        console.log('should have saved')
    } 
});

私のphpスクリプトは次のようになります。

<?php
$xml = $_POST['data'];
$file = fopen("data/data.xml","w");
fwrite($file, $xml);
fclose($file);
echo "ok";
?>

多分私は何かが欠けているかもしれませんし、最初にxmlをエンコードする必要があるかもしれませんが、これの例を見つけることができません. ただし、ロードされたように見えて成功のコールバックが返されたが、xml ファイルが空になりました。

4

1 に答える 1

0

私はあなたが必要とするものをまったくキャッチしたかどうかわかりませんが、あなたはすべきです:

アヤックス

     $.ajax({
        type: "GET",
        url: "data/data.xml",
        dataType: "xml",
        error:function(response){ 
        console.log(response);
         },
        success: function (xml) {

            init(xml); 
        }
    });

 function init(xml){
         $.ajax({ 
                url: "saveXml.php", 
                type: "POST", 
                contentType: "text/xml", 
                dataType:'xml',
                processData: true, 
                data: {'xml':xml},
                error:function(response){ 
                console.log(response);
                },
                success: function(data){
                    console.log(data); //check your console.log now
                } 
            });
    }

PHP

 <?php
    $xml = $_POST['xml'];
    //you don't need to open the file, you have the source of the file in $_POST['data']
    echo $xml;
    ?>
于 2012-11-23T09:23:00.197 に答える