When attempting to send an array via AJAX using JSON, I am encountering a problem where my C# handler is unable to properly handle the query. It seems that the querystrings are merging together inexplicably.
In the scenario presented here, I am trying to send a basic array to the server, but the server is reporting that the querystring Name is null (even though it isn't); Sending requests without the array work as expected.
I would greatly appreciate it if someone could provide insight into how the array appears in the URL (in case I wanted to manually send a request through the browser).
AJAX code:
function btnClick() {
var arr = new Array();
arr[0] = "Hey";
arr[1] = "Stackoverflow";
arr[2] = "What's your name?";
var jsonParam = { Name: "test", Pass: "123", Stuff: arr }
$.ajax({
url: "Test.ashx",
type: "get",
data: JSON.stringify(jsonParam),
dataType: "json",
contentType: 'application/json; charset=utf-8',
async:false,
success: function (response) {
alert(response.Name);
}
});
}
Handler code:
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "application/json";
JavaScriptSerializer jss = new JavaScriptSerializer();
string res = jss.Serialize(new UserInfo { Name = context.Request.QueryString["Name"], Pass = "pass" + context.Request.QueryString["Pass"], Stuff = new string[] { "1", "2" } });
context.Response.Write(res);
}