zoukankan      html  css  js  c++  java
  • net jquery jsonp跨域请求

    资料:http://www.dotblogs.com.tw/topcat/archive/2011/09/21/37024.aspx

    1.在A网站构建ashx

    可以看到请求时这样子的  www.xxx.com/xx.ashx?callback=?

    输出:?({"UserName":"0010001BKCVXGJV","PassWord":"KHFTGUBY"})

    public class GetAccountIDHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string callback = context.Request.QueryString["callback"];
            if (!string.IsNullOrEmpty(callback))
            {
                AccountUser user = new AccountUser();
                user.UserName =“admin“;
                user.PassWord = “admin“;;
                string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(user);
                context.Response.Write(callback+"(" + jsonString + ")");  
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public class AccountUser
        {
            public string UserName { get; set; }
    
            public string PassWord { get; set; }
    
        }
        
    }

    2.网站B:请求端

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <!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>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
        <script language="javascript" type="text/javascript">
            $(document).ready(function() {
                $('#btn1').click(btn1_click);
            });
            function btn1_click() {
                $.getJSON('http://localhost:32330/WebSite/portal/GetAccountIDHandler.ashx?callback=?', jsonp_callback);
            }
            function jsonp_callback(data) {
                alert(data.UserName);
                alert(data.PassWord);
            }
    
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="btn1" type="button" value="取得" />
        </div>
        </form>
    </body>
    </html>
  • 相关阅读:
    python获取当前路径
    python的StringIO
    python判断两个文件是否相同
    Linux查找文件内容
    python日志syslog运用
    python获取当前运行程序的名字
    python连接Linux命令行
    python预编译函数compile,exec,eval
    python日志模块
    Scala安装教程
  • 原文地址:https://www.cnblogs.com/0banana0/p/2653825.html
Copyright © 2011-2022 走看看