利用jquery,访问ashx,调用ironPython模块功能

看到网上有个帖子是关于vs调用ironPython的,想到后面的逻辑完全可用python来完成,前面的部分就用html+jquery来实现!

试一下成功,留下来,以后参考!

ASHX文件主要代码

  public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            dynamic obj = pyRunTime.UseFile(HttpContext.Current.Request.PhysicalApplicationPath+ (@"/hello.py"));
            string action=context.Request["action"];
            switch (action)
            {
                case "ab":
                    context.Response.Write(obj.welcome("ab响应处理"));
                    break;
                case "ac":
                    context.Response.Write(obj.welcome("ac响应处理"));
                    break;
                default:
                    context.Response.Write("你什么也没有输入!");
                    break;
            }
        }

HTML及jquery脚本

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>ashx处理</title>
    <script src="Scripts/jquery-2.2.0.min.js"></script>




</head>
<body>
    <input id="Text1" type="text" value="ab" />
    <div id="context">
    </div>
    <input id="submit" type="button" οnclick="send()" value="点我提交" />
</body>
</html>
<script>
    $(document).ready();
    function send() {
        var da = $("#Text1").val();
        $.ajax({
            url: 'Handler1.ashx',
            type: 'POST',
            data: { action: da },
            dataType: 'text',
            timeout: 1000,
            error: function () { alert('Error loading PHP document'); },
            success: function (result) { $("#context").html("<h1>" + result + "<h1/>"); }
        });
    }
</script>

PY模块文件 

# -*- coding: utf-8 -*-
# 第一行的目的,是为了让代码里面,可以有中文注释信息. (否则要运行报错)
# 这个 Python 脚本, 用于被 C# 来调用.
# 简单测试 Hello World 的效果.
def welcome(name):
    return "hello " + name


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值