<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="../Scripts/jquery-1.7.2.min.js"></script>
    <script src="../Scripts/JSON-js-master/json2.js"></script>
    <script type="text/javascript">

        $(function () {

            $.ajax({
                type: "GET",
                dataType: "jsonp",//jsonp,json
                //contentType: "application/json; charset=utf-8;",
                async: true,
                jsonp: 'callback',     //传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback)          
                jsonpCallback: 'jsonpCallback',//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名
                //url: 'GetLocation.ashx',
                data: { name: 'zdl' },              
                url: 'http://localhost:8099/GetLocation.ashx',                      
                success: function (jsonData) {
                    console.log(jsonData); 
                    //alert(JSON.stringify(jsonData));
                    var a = JSON.stringify(jsonData);
                    console.log(a);
                    alert(jsonData.message);
                    alert(1);
                },
                error: function (request, textStatus, errorThrown) {
                    //console.log(request.responseText);
                    //console.log(textStatus);
                    //console.log(errorThrown);
                    alert(2);
                }
            });

        
        
        })

       
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

  

<%@ WebHandler Language="C#" Class="GetLocation" %>

using System;
using System.Web;

public class GetLocation : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {

        #region 方式1 
        //string callback01 = context.Request["callback"];
        //context.Response.Write(callback01 + "( [ { message:\"John\"} ] )");
        #endregion

        #region 方式2 
        //string callback02 = context.Request["callback"];
        //context.Response.Write(callback02 + "( { message:\"John\"}  )");
        #endregion

        #region 方式3
        var callback03 = context.Request["callback"];  //获得请求端回调函数
        var data = "{" + "message:'" + context.Request["name"] + "'," + "id:'0001'" + "}";
        var jsonp = callback03 + '(' + data + ')';
        context.Response.Write(jsonp);
        #endregion
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

  参考:http://www.cnblogs.com/know/archive/2011/10/09/2204005.html

 posted on 2017-09-07 21:28  dianli  阅读(84)  评论(0编辑  收藏  举报