The use of jQuery's 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",//The parameter name passed to the request handler or page to get the name of the jsonp callback function (default: callback)
            jsonpCallback:"success_jsonpCallback",//custom jsonp callback function name, the default is the random function name automatically generated by 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;
        }
    }

}

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326837606&siteId=291194637