abc abc - 4 months ago 10
jQuery Question

accessing bulk array values in code behind using ajax

passing values to code behind (cs).I had researched a lot and used this code but it didn't worked for me below is the code that I used what I need is I need to pass values in code behind(CS) 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 and I am getting a button when I run this code when I click this not happens except the page is getting refreshed each time when I click the button can any one help me out in doing this

Answer

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
Comments