简体   繁体   中英

Accessing bulk array values in code behind using Ajax

I am passing values to code behind (C#). I had researched a lot and used this code, but it didn't work for me. Below is the code that I used.

I need to pass values in code behind (C#), but using this code I am getting an error near routes.EnableFriendlyUrls(settings); as:

Script:

<script>
    function foo() {
       var values = ["1,", "2", "3"];

       // Make the Ajax call
       $.ajax({
           type: "POST",
           url: "Default.aspx/Done", // the method we are calling
           contentType: "application/json; charset=utf-8",
           data: {values : JSON.stringify({ arr: values })},
           dataType: "json",
           success: function (result) {
               alert('Yay! It worked!');
           },
           error: function (result) {
               alert('Oh no :(');
           }
       });
       return false;
    }
</script>

HTML:

<form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button"  UseSubmitBehavior="false" OnClientClick="return foo();" />
    </div>
</form>

Code:

    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        //settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
    }
}

[WebMethod]
public static void test(string[] arr)
{

}

Though I had an error in code behind, I am not getting any error, and I am unable to go to code behind even after putting the break point. I am getting a button when I run this code, and when I click this not happen except the page is getting refreshed each time when I click the button. How can I do this?

Change:

 var values = [1, 2, 3];//your array wasn't valid 

 data: {values : values},//you don't need to stringify the values because we have a valid json syntax

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM