【请教】在C#中使用ajax,并读取返回值 大家好,我有以下疑问,请指教: 在客户端使用Ajax并读取返回值是很容易的;在服务端(C#)注册JS代码也可以理解。 那么,可否将两者结合起来,在C#中使用Ajax并读取其返回值呢?谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 .NET 有自带的AJAX的框架 不仅可以实现你说的 还可以在请求的开始 结束的时候截获请求! 谢谢您的回复!之前也稍微用过这个.NET自带的Ajax框架(好像是叫ScriptManager的东东吧),但给我的感觉好像是必须要与ASPX页面进行互动(集成)操作的吧---不大清楚,先把疑问放在这里,之后再去查查。而我现在是想直接在C#中(比如就新建一个CS文件),使用这种方式,不知是否可行呢? 相对于aspx界面的后台.cs文件中使用Response.Write();输出。然后在另一个界面用Ajax代码接收即可Ajax的URL直接指向要调用的aspx页面 谢谢您的回复!不过,我是想通过使用多次Ajax进行批处理后使用返回的多次结果,您这种方式不大可能吧?望再次赐教 举一个具体的应用:第一步、 使用谷歌地图的API(Ajax),根据地址来获取坐标。 这是通过其内部的Ajax方式来完成的,对单个地址倒很容易,直接在回调方法中可以获取坐标值; 但是,如果是想对多个地址(甚至是成百上千的大数据量)进行坐标获取,那么这里应该怎么实现为好呢?第二步、服务端实现。 第一步是在JS中使用的方式,那么,如果这个功能可以实现的话,在C#代码中又应该怎样来实现呢? 文件列表:文件路径 服务器/html/ajax.html 192.168.1.94/ashx/ajax.ashx 192.168.1.102csdn 论坛上朋友的热心提醒与帮助以及大量的搜索资源,以下是一些相关的参考网址:http://wenku.baidu.com/view/35c684868762caaedd33d44b.htmlhttp://topic.csdn.net/u/20081224/09/26aff992-d0ba-4f6f-a023-e0e96ecbe1fc.html代码片断如下:表单页面代码(路径:/html/ajax.html):<!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> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /></head><body> <form name="form1" action=""> <div>User Name:<input type="text" id="company" onkeyup="getCompany(this, 'showCompany')" /></div> <div>Suggestions: <span id="content"></span></div> </form></body></html><script type="text/javascript"> function showCompany(val) { document.getElementById('content').innerHTML = val; }</script><script type="text/javascript"> /* obj 表单对象 func 回调函数名称的字符串形式 */ function getCompany(obj, func) { var url = 'http://192.168.1.102/ashx/ajax.ashx'; //另一个服务器上的处理页面 url += '?sid='+ (new Date()).getTime(); url += '&func=' + func; url += '&value=' + obj.value; var id = "ajax"; //往表头添加 script 标签时,id 的属性值 var ajax = document.getElementById(id); var head = document.getElementsByTagName('head').item(0); if (ajax) { head.removeChild(ajax); } ajax = document.createElement('script'); ajax.setAttribute("src", url); ajax.setAttribute("id", id); ajax.setAttribute("type", 'text/javascript'); ajax.setAttribute("languge", 'javascript'); head.appendChild(ajax); return ajax; }</script>ajax处理页面代码(路径:/ashx/ajax.ashx):<%@ WebHandler Language="C#" Class="ajax" %>using System;using System.Web;public class ajax : IHttpHandler { public void ProcessRequest (HttpContext context) { string text = "text/plain"; string func = context.Request.QueryString["func"].ToString(); string value = context.Request.QueryString["value"].ToString(); string outer = string.Format("{0}('{1}');", func, value); context.Response.ContentType = text; context.Response.Write(outer); } public bool IsReusable { get { return false; } }}刚刚Ctrl+C来的. 还有就是返回的一般就是字符串或者是xml格式的.然后或者遍历.或者是特殊格式.比如json.就可以返回多个值了...呵呵.我也是在学习啊学习着. C# , 一句话的解释,请指教 今天面试的题目 PerformanceCounter使用问题 高手来帮帮忙 [单元测试]我要测试返回的IList 有什么好用的 ASP.NET 论坛代码吗? 关于byte数组元素相互赋值的问题!~!!! 关于通讯簿的问题,请高手进来一下,谢谢!!! 【高分】调用COM时的错误,请高手指点一下 新手上路,请多关照,; ) 求助!!关于dev TreeList控件的问题,很郁闷。请高手帮忙解决下! Winform里的reportViewer控件的问题
然后在另一个界面用Ajax代码接收即可
Ajax的URL直接指向要调用的aspx页面
这是通过其内部的Ajax方式来完成的,对单个地址倒很容易,直接在回调方法中可以获取坐标值;
但是,如果是想对多个地址(甚至是成百上千的大数据量)进行坐标获取,那么这里应该怎么实现为好呢?第二步、服务端实现。
第一步是在JS中使用的方式,那么,如果这个功能可以实现的话,在C#代码中又应该怎样来实现呢?
/html/ajax.html 192.168.1.94
/ashx/ajax.ashx 192.168.1.102csdn 论坛上朋友的热心提醒与帮助
以及大量的搜索资源,以下是一些相关的参考网址:
http://wenku.baidu.com/view/35c684868762caaedd33d44b.html
http://topic.csdn.net/u/20081224/09/26aff992-d0ba-4f6f-a023-e0e96ecbe1fc.html代码片断如下:表单页面代码(路径:/html/ajax.html):<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
<form name="form1" action="">
<div>User Name:<input type="text" id="company" onkeyup="getCompany(this, 'showCompany')" /></div>
<div>Suggestions: <span id="content"></span></div>
</form>
</body>
</html>
<script type="text/javascript">
function showCompany(val)
{
document.getElementById('content').innerHTML = val;
}
</script>
<script type="text/javascript">
/*
obj 表单对象
func 回调函数名称的字符串形式
*/
function getCompany(obj, func)
{
var url = 'http://192.168.1.102/ashx/ajax.ashx'; //另一个服务器上的处理页面
url += '?sid='+ (new Date()).getTime();
url += '&func=' + func;
url += '&value=' + obj.value;
var id = "ajax"; //往表头添加 script 标签时,id 的属性值
var ajax = document.getElementById(id);
var head = document.getElementsByTagName('head').item(0);
if (ajax)
{
head.removeChild(ajax);
}
ajax = document.createElement('script');
ajax.setAttribute("src", url);
ajax.setAttribute("id", id);
ajax.setAttribute("type", 'text/javascript');
ajax.setAttribute("languge", 'javascript');
head.appendChild(ajax);
return ajax;
}
</script>ajax处理页面代码(路径:/ashx/ajax.ashx):<%@ WebHandler Language="C#" Class="ajax" %>
using System;
using System.Web;
public class ajax : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
string text = "text/plain";
string func = context.Request.QueryString["func"].ToString();
string value = context.Request.QueryString["value"].ToString();
string outer = string.Format("{0}('{1}');", func, value);
context.Response.ContentType = text;
context.Response.Write(outer);
}
public bool IsReusable {
get {
return false;
}
}
}刚刚Ctrl+C来的.