0

ユーザーが複数のキーワードを送信してから、それらのキーワードに基づいてリソースを検索するページがあります。すべてのキーワードをロードするjQuery配列があり、そのjQuery配列を次の結果ページに渡したいと思います。私はこれを行うための最良の方法を見つけようとしています。配列を文字列に入れてURLに渡そうとしましたが、URLを正しくエンコードできませんでした。window.locationも使用してみましたが、これもうまく機能していません。これは簡単に思えますが、私は輪になって行きます。これは、メモリに保持して次のaspxページに渡す1次元配列です。最良の方法は何ですか?以下は、現在の私のコードですが、正しくありません。

//When get resources button clicked show current array values
$("#nihSearch_btnResults").click(function () {
    //get number of tabs in the holder
    gettabs = $("div#keys > div.tab").size();
    output = "";
    //loop through the tabs        
    $("div.tab span").each(function () {
        //get the text of the tab
        var kw = $(this).text();
        //add it to the array
        keywords.push(kw);
    });

    var jsonstring = JSON.stringify(keywords);
    alert(jsonstring);
});
4

2 に答える 2

0

URL経由でできました。配列を文字列に変換し、すべてのスペースをアンダースコアに置き換えてから、window.location を使用して次のページに移動しました。そこで、C# ページで Page_Load を使用してアンダースコアをスペースに戻し、「分割」を使用して配列を再作成します。

jQuery

//convert array into a string
    var keystring = keywords.join(",");
    //make keystring all lower case and replace spaces with hyphens
    keystring = keystring.toLowerCase().replace(/ /g, '_');
    //Send it to the next page.
    window.location = "Results.aspx?kws=" + keystring;

C#

protected void Page_Load(object sender, EventArgs e)
    {
        //get keywords from URL
        string urlkeys = Request.QueryString["kws"];
        //replace dashes with spaces
        urlkeys = urlkeys.Replace("_", " ");
        //split back into an array
        string[] arr = urlkeys.Split(',');            
    }
于 2012-05-16T18:59:02.530 に答える
0

$.ajaxメソッドとasp.netサーバー側を使用して、クライアント側のデータをサーバーに投稿できますweb-method

これが私が実装したコードです
http://blog.nitinsawant.com/2011/09/draft-sending-client-side-variables-to.html

         //js code to post data to server
         var jsonData = "{'jsonData':'" + JSON.stringify(keywords) + "'}";//create string representation of the js object
         $.ajax({
            type: "POST",
            url: 'Test.aspx/AcceptData',
            data: jsonData,
            contentType: "application/json; charset=utf-8",
            dataType: ($.browser.msie) ? "text" : "json",
            success: function(msg) {
                //call successfull
                var obj = msg.parseJSON();
                alert(obj.d); //d is data returned from web services

                //The result is wrapped inside .d object as its prevents direct execution of string as a script
            },
            error: function(xhr, status, error) {
                //error occurred
                alert(xhr.responseText);
            }
        });

ウェブ方式:

//C# code
[System.Web.Services.WebMethod]
 public static string AcceptData(object jsonData)
 {
     Customer newCust =(Customer)JsonConvert.DeserializeObject(jsonData.ToString(),typeof(YourCustomClass));
     return "Server response: Hello "+newCust.FirstName;
 }

よろしく
ニティン

于 2012-05-17T09:03:12.573 に答える