看到网上有个帖子是关于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