zoukankan      html  css  js  c++  java
  • Ajax跨域请求ashx文件与Webservice文件

    前台页面:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
    
    <!DOCTYPE html>
    
    <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="jquery-1.7.1.js"></script>
        <script type="text/javascript">
            jQuery(function () {
                jQuery.ajax({
                    url: "http://localhost:1203/Handler1.ashx?callback=?",
                    //jsonpCallback:callback,
                    dataType: "jsonp",
                    data: { name: "likong" },
                    success: function (result) {
                        window.alert("姓名:" + result.name + ", 性别:" + result.gender);
    
                    }
    
            })
    
            function callback(){
                window.alert("回调成功!");
            
            }
    
            })
        </script>
    </head>
    <body>
    </body>
    </html>
    View Code

    ashx文件代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace WebApplication1
    {
        /// <summary>
        /// Handler1 的摘要说明
        /// </summary>
        public class Handler1 : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                string callback = context.Request.QueryString["callback"];
                 var name = context.Request.QueryString["name"];
                 string json = "{"name":"" + name + "","gender":"" + "" + ""}";
                 //JSONP格式:回调函数名(json格式参数)
                 //括号后不要加分号
                 string result = callback + "(" + json + ")";
                 context.Response.ContentType = "application/json";
                 context.Response.Write(result);
    
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    View Code

     前台代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
    
    <!DOCTYPE html>
    
    <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="jquery-1.7.1.js"></script>
        <script type="text/javascript">
            jQuery(function () {
                jQuery.ajax({
                    url: "http://localhost:1203/WebService1.asmx/GetGenderByName?callback=?",
                    //jsonpCallback:callback,
                    dataType: "jsonp",
                    data: { name: "李空" },
                    success: function (result) {
                        window.alert("姓名:" + "" + result.name + ", 性别:" + result.gender);
                    }
    
    
                });
    
            function callback(){
                window.alert("回调成功!");
            
            }
    
            })
        </script>
    </head>
    </html>
    View Code

    或者:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
    
    <!DOCTYPE html>
    
    <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="jquery-1.7.1.js"></script>
        <script type="text/javascript">
            jQuery(function () {
                jQuery.getJSON("http://localhost:1203/WebService1.asmx/GetGenderByName?callback=?&name=李空", function (result) {
                    window.alert("姓名:" + "" + result.name + ", 性别:" + result.gender);
                }, callback())
    
            function callback(){
                window.alert("回调成功!");
            
            }
    
            })
        </script>
    </head>
    </html>
    View Code

    Webservice代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.Script.Serialization;
    using System.Data;
    
    namespace WebApplication1
    {
        /// <summary>
        /// WebService1 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
         [System.Web.Script.Services.ScriptService]
        public class WebService1 : System.Web.Services.WebService
        {
    
          [WebMethod]
             public void GetGenderByName(string callback, string name)
             {
                 string json = "{"name":"" + name + "","gender":"" + "" + ""}";
                 string result = callback + "(" + json + ")";
                 HttpContext.Current.Response.ContentType = "application/json";
                 HttpContext.Current.Response.Write(result);
                 HttpContext.Current.Response.End();
             }
        }
    }
    View Code

    注释:测试Webservice是否调用成功,必须先运行Webservice页面!

  • 相关阅读:
    A. Dawid and Bags of Candies ( Codeforces Round #588 (Div. 2) )
    B. Ania and Minimizing (Codeforces Round #588 (Div. 2) )
    残缺的棋盘 (BFS)
    Max Sum (动态规划)
    高桥和低桥 (离散化 )
    White Sheet (矩形面积模板) (Codeforces Round #587 (Div. 3) )
    Catch That Cow (BFS luo搜 + 剪枝)
    Python笔记-字符串
    关于拖延症
    一些告诫
  • 原文地址:https://www.cnblogs.com/2013likong/p/3473256.html
Copyright © 2011-2022 走看看