`
liyonghui160com
  • 浏览: 761643 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

jQuery的ajax jsonp的使用

阅读更多

 

 

 

<!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>
    <title>Untitled Page</title>
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
     <script type="text/javascript">
    jQuery(document).ready(function(){
        $.ajax({
            type : "get",
            async:false,
            url : "ajax.ashx",
            dataType : "jsonp",
            jsonp: "callbackparam",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback)
            jsonpCallback:"success_jsonpCallback",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名
            success : function(json){
                alert(json);
                alert(json[0].name);
            },
            error:function(){
                alert('fail');
            }
        });
        var a="firstName Brett";
        alert(a);
    });
    </script>
    </head>
 <body>
 </body>
</html>

 

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

using System;
using System.Web;

public class ajax : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string callbackFunName = context.Request["callbackparam"];
        context.Response.Write(callbackFunName + "([ { name:\"John\"} ] )");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics