0

JS / jQueryを使用して(複雑な)配列(または必要に応じてオブジェクト)としてサーバーに送信する形式の情報がいくつかあります。(jQuery 1.7.2)問題をスマートに解決するためにJSONについて考えています。私のコードは現在機能していますが、それをより良くすることが可能かどうか知りたいです。

したがって、この例はかなり典型的です(データはより複雑なirlです):

dataSend = { 'id': '117462', 'univers': 'galerie', 'options' : { 'email': 'hello@world.com', 'commenataire': 'blabla', 'notation' : '4' } };

$.ajax({
  url: "/ajax/ajaxMavilleBox.php",
  data: JSON.stringify(dataSend),
  success: function(x){
      smth();
  }
});

別のコンテキストでは、JSONなしでまったく同じものを作成する必要があります。

同じ例で:

dataSend = { 'id': '117462', 'univers': 'galerie', 'options' : { 'email': 'hello@world.com', 'commenataire': 'blabla', 'notation' : '4' } };

$.ajax({
    url: "/ajax/ajaxBox.php",
    data: $.param(dataSend),
    success: function(x){
        smth();
    }
});

明らかに、私は何かが欠けています。

URLは:

http://www.mywebsite.com/ajax/ajaxBox.php?id=117462&univers=galerie&options=%5Bobject+Object%5D

そして、URLは次のようになります:

http://www.mywebsite.com/ajax/ajaxBox.php?id=117462&univers=galerie&options[email]=hello@world.com&options[commenataire]=blabla&options[notation]=3

それを行う簡単な方法はあります(ループなどで自分でデータを編集する必要がないことを願っています)

編集:2番目の部分の解決策

OK、JSONのない最後の部分は正しいです。実際、私は自分のページで古いバージョンのjQueryを使用していました。$ .paramは、jQuery<1.4ではあまり良くありません

詳細はこちらParamDoc

4

2 に答える 2

3

I would suggest setting the type: 'POST', otherwise you will have a data limit eqvivalent of the browsers query string length.

If you use the post method, you should send the data as a json-string. Something like:

data: { DTO: JSON.stringify(dataSend) }

You need to use json2.js if window.JSON is undefined (like in ie7 for example).

If you are using PHP on the serverside, you can fetch the object using:

$data = json_decode($_POST['DTO']); //will return an associative array

or ASP.NET

public class DataSctructure
{
    public string id { get; set; }
    public string univers { get;set; }
    //etc...
}

var data = HttpContext.Current.Request.Form['DTO'];

DataSctructure ds = new JavaScriptSerializer().Deserialize<DataSctructure>(data);

//properties are mapped to the ds instance, do stuff with it here
于 2013-01-24T14:45:49.297 に答える
0

@Johanが述べたように、 使用しているブラウザの開発者オプションを使用して送信するデータを表示するPOST代わりに、ここで使用する必要があります。f12を押すだけ で、URLで誤ったシリアル化された文字列を使用 していることを確認してください。1.4バージョンより前のシリアル化GET

jquery >= 1.4
$.param()

于 2013-01-24T14:51:50.690 に答える