0

I have a page where a user submits multiple keywords before finding resources based on those keywords. I have a jQuery array that loads all the keywords and I want to pass that jQuery array to the next results page. I'm trying to figure out the best method to do this. I've tried putting the array into the string and passing it through the URL, but was unable to properly URLEncode it. I've also tried using window.location but this has been not working well either. This seems simple but I'm going in circles. It is a one dimensional array that I want to hold in memory then pass it to the next aspx page. What's the best method? Below is my code as it stands now which is not correct.

//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

You can post the client side data to server using $.ajax method and asp.net server side web-method

Here's the code which I had implemented
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);
            }
        });

web method:

//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;
 }

Regards
Nitin

于 2012-05-17T09:03:12.573 回答
0

我能够通过 URL 做到这一点。我将数组转换为字符串,用下划线替换所有空格,然后使用 window.location 移动到下一页。在那里,我使用 C# 页面中的 Page_Load 将下划线替换回空格,并使用“split”重新创建数组。

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 回答