(Question resolved with the help of the two reply posts--see below)
I would appreciate help getting a simple example of exchanging data JSON data between a browser (
Other resource suggest removing the contentType: 'application/json; charset=utf-8',
from the AJAX call:
$.ajax({
url: "ericHandler.ashx",
data: myData,
dataType: 'json',
type: 'POST',
success: function (data) { alert("DIDit = " + data.eric); },
error: function (data, status, jqXHR) { alert("FAILED:" + status); }
});
Read the values on the server side:
string myPar = context.Request.Form["par"];
You can also try:
string json = new StreamReader(context.Request.InputStream).ReadToEnd();
which was mentioned here: https://stackoverflow.com/a/8714375/139917
I got this working right away. This is its most basic form:
HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$.ajax({
url: 'Handler.ashx',
type: 'POST',
success: function (data) {
alert(data);
},
error: function (data) {
alert("Error");
}
});
});
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
Code Behind:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}
public bool IsReusable
{
get
{
return false;
}
}
}