JSONP简单示例


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>无标题页</title>
     <script type="text/javascript">
         var script = document.createElement("script");
         script.type = "text/javascript";
         script.src = "IbeaconHandler.ashx?op=callBack";
         document.getElementsByTagName("head")[0].appendChild(script);
         //回调函数
         function callBack(jsonp) {
             alert(jsonp.name);  //judy
         }
    </script>
</head>
<body>
</body>
</html>

后台代码:
<%@ WebHandler Language="C#" Class="IbeaconHandler" %>

using System;
using System.Web;

public class IbeaconHandler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string fname = context.Request.QueryString["op"].ToString();
        string str =fname+"({name:'judy',age:'23'})";
        context.Response.Write(str);
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}
原文地址:https://www.cnblogs.com/zcttxs/p/3179590.html