1

さて、私はJSON文字列をPHPページに投稿する際にいくつかの自殺的な問題を抱えています. 私は文字通り、Google のトップ 10 の結果と、私の問題に関連する多くの SO の質問を経験しましたが、それでも私が間違っていることを解決できません。

ページに複数のフォームがあり、すべてのフォーム フィールドを収集し、それらを JSON 文字列に変換して、スクリプトが各項目を反復し、関連するデータベース テーブルを更新する PHP ページに投稿したいと考えています。

これは、すべてのフォームからデータを収集するための jQuery/JS スクリプトです。

var photo_annotations = {};

$('form').each(function(i) {
    var id = $(this).attr('id');
    photo_annotations[id] = {
        caption: $('#'+id+'_caption').val(),
        keywords: $('#'+id+'_keywords').val(),
        credit: $('#'+id+'_credit').val(),
        credit_url: $('#'+id+'_credit_url').val()
    };
});

photo_annotations オブジェクトの場合console.log、2 つのフォームの例に基づいて、次のように生成されます。

({11:{caption:"最初の写真のキャプション。"、keywords:"Keyword1, Keyword2, Keyword3", credit:"Joe Bloggs", credit_url:"www.a-domain.com"}, 12:{caption: "レディー ガガのキャプション。"、keywords:"Keyword3, Keyword4"、credit:"John Doe"、credit_url:"www.another-domain.com"}})

次に、これを文字列/JSON として PHP ページに POST する必要があるため、次のようにしました。

$.ajax({
    type: 'POST',
    dataType: 'html',
    url: 'ajax/save-annotations.php',
    data: { data: JSON.stringify(photo_annotations) },
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        if (data) {
        $('#form_results').html(data);
        } else {
        alert("No data");   
        }
    }
});

そして私のPHPページには、これがあります:

<?php
//print_r($_POST['data']);
$decoded = json_decode($_POST['data'],true);
print_r($decoded);
?>

さて、私が試したのはこれだけではありません。純粋な文字列だけを送信するために、AJAX スクリプトからすべての JSON 設定を削除しようとしました。contentType と JSON.stringify を削除しようとしましたが、それでもうまくいきません。私の PHP ページは、送信しているデータを取得できません。

正しい方向に私を押してください。試したすべてのバリエーションを思い出すことができないところまで来ました。この小さなスクリプトは現在 2 日目です。

それを修正するために管理

AJAX 関数を書き直したところ、機能しました。何が問題なのかわかりませんが、非常に基本的なデータ文字列test=hello worldを使用して AJAX 関数をテストすることにし、PHP ページから POST データを読み取ることができないことがわかりました。私は送った。非常に奇妙な。とにかく、これは改訂された AJAX スクリプトです。

var the_obj = JSON.stringify(photo_annotations);
var post_data = "annotations="+the_obj;
$.ajax({
    url: 'ajax/save-annotations',
    type: 'POST',
    data: post_data,
    dataType: 'html',
    success: function(data) {
        $('#form_results').html(data);
    }
});
4

4 に答える 4

2

Try:

  $.ajax({
    // ...
    data: { data: JSON.stringify(photo_annotations) },
    // ...
  });

If you just set the "data" property to a string, then jQuery thinks you want to use it as the actual query string, and that clearly won't work when it's a blob of JSON. When you pass jQuery an object, as above, then it'll do the appropriate URL-encoding of the property names and values (your JSON blob) and create the query string for you. You should get a single "data" parameter at the server, and it's value will be the JSON string.

于 2013-09-14T13:12:24.323 に答える
0

次のようにurldecodeまたはrawurldecodeを試してください。

<?php
$decoded = json_decode(urldecode($_POST['data']), true);
print_r($decoded);
?>
于 2013-09-14T13:45:09.493 に答える
0

AJAX 関数を書き直したところ、動作するようになりました。何が問題なのかわかりませんが、非常に基本的なデータ文字列test=hello worldを使用して AJAX 関数をテストすることにし、PHP ページから POST データを読み取ることができないことがわかりました。私は送った。非常に奇妙な。とにかく、これは改訂された AJAX スクリプトです。

var the_obj = JSON.stringify(photo_annotations);
var post_data = "annotations="+the_obj;
$.ajax({
    url: 'ajax/save-annotations',
    type: 'POST',
    data: post_data,
    dataType: 'html',
    success: function(data) {
        $('#form_results').html(data);
    }
});

私が考えることができる唯一のことは、AJAX設定の順序が特定の順序である必要があるということです. これは、POST データを正常に送信しない私の古い AJAX スクリプトです。送信はできますが、読み取ることができません!!

var the_obj = JSON.stringify(photo_annotations);
var data_str = "annotations="+the_obj;
$.ajax({
    type: 'POST',
    dataType: 'html',
    data: data_str,
    url: 'ajax/save-annotations.php',
    success: function(data) {
        $('#form_results').html(data);
    }
});
于 2013-09-14T22:37:34.547 に答える
0

あなたのajax呼び出しで、dataTypeをjsonにリセットしてみてください

dataType: "json",

JSON.stringify() も使用する必要はありません。PHP スクリプトでは、$_POST 変数からのデータを [json_decode()] でデコードする必要はありません。データは、php スクリプトで簡単に読み取ることができます。

于 2014-04-18T12:17:59.017 に答える