Skip to content

Instantly share code, notes, and snippets.

@joeldart
Created March 3, 2011 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeldart/852309 to your computer and use it in GitHub Desktop.
Save joeldart/852309 to your computer and use it in GitHub Desktop.
/****************jsonpcall.aspx*********************************/
public partial class jsonpcall : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.QueryString["callback"] + "('JSONP SUCCESS')");
Response.End();
}
}
}
/****************isvalid.aspx************************************/
public partial class isvalid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("true");
Response.End();
}
}
/******************jsoncall.aspx********************************/
protected void Page_Load(object sender, EventArgs e)
{
if(Request.ContentLength > 0 && new System.IO.StreamReader(Request.InputStream).ReadToEnd().Contains("foo")){
Response.Write("{\"response\": \"AJAX SUCCESS!\"}");
Response.End();
}
}
/****************default page************************************/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<input type="text" class="required" name="myname" id="myname" />
<input type="submit" value="Submit"/>
</form>
<div id="results"></div>
<div id="jsonpresults"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
(function () {
var old = jQuery,
ajaxDefaults = jQuery.extend({}, jQuery.ajaxSettings);
delete ajaxDefaults.jsonp;
delete ajaxDefaults.jsonpCallback;
jQuery = jQuery.sub();
jQuery.oldjQuery = old;
//set up defaults with no jsonp
jQuery.ajaxSettings = ajaxDefaults;
}());
</script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery.validate.js" type="text/javascript" ></script>
<script type="text/javascript">
//move validate over and switch back
jQuery.oldjQuery.fn.validate = jQuery.fn.validate;
jQuery = jQuery.oldjQuery;
</script>
<script type="text/javascript">
$("#form1").validate({
rules: {
myname: {
required: true,
remote: "isvalid.aspx"
}
}
});
$.ajax({
contentType: "text/json",
data: JSON.stringify({ "foo": "bar" }),
dataType: "json",
type: "POST",
url: "jsoncall.aspx",
success: function (data) {
$("#results").text(data.response);
}
});
$.ajax({
url: "jsonpcall.aspx",
dataType: "jsonp",
success: function (data) {
$("#jsonpresults").text(data);
}
});
console.log("oldjQuery" in jQuery);
console.log("jsonpCallback" in jQuery.ajaxSettings);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment