I'm new to the world of ajax requests and asp.net. I'm trying to send an ajax request to aspx page. When I'm debugging the server side it seems to be ok, but the response printing the error message? I tried to change the Response.ContentType but it didn't solve my problem. Any suggestions?
Here is my javascript code:
function onclick(){
$.ajax({
url: "SandaAJAXRequests.aspx",
type: "post",
data: JSON.stringify({ "first": "getevent","second":"data" }),
dataType: 'json',
success:
function (response)
{
roundNumber = 1;
numberofblackwins = 0;
numberofredwins = 0;
ifBattleIsOver = false;
$("#round").text(roundNumber);
var result = response.split(",");
name1 = result[1];
name2 = result[2];
var r = confirm("Is " + name1 + " the black competitor?");
if (r == true) {
$("#black_competitor_name").text(name1);
$("#red_competitor_name").text(name2);
}
else {
$("#black_competitor_name").text(name2);
$("#red_competitor_name").text(name1);
}
},
error: function (xhr) {
alert("Problem sending data to the server");
}
});
}
Here is my server side:
protected void Page_Load(object sender, EventArgs e)
{
string jsonString = String.Empty;
Request.InputStream.Position = 0;
using (var inputStream = new StreamReader(Request.InputStream))
{
jsonString = inputStream.ReadToEnd();
}
string data = jsonString.Replace("\\", "");
char[] seperators = { ':', ',', '"' };
string[] a = data.Split(seperators);
string t = a[4];
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
object serJsonDetails = javaScriptSerializer.Deserialize(jsonString, typeof(object));
string requestType = serJsonDetails.ToString();
switch (t)
{
case "getevent":
SandaEvent current = (SandaEvent)Application["CurrentEvent"];
Response.ContentType = "text/plain";
String response = current.id + "," + current.name1 + "," + current.name2;
Response.Write(response);
Response.End();
break;
}}