关于js调用ashx数据

风隐若现 2015-07-24 10:07:58
我想在ashx中编辑json数据 Id = 1001,name= "zhangyu",score = 87
然后通过js访问这些数据, 写个alert消息提示也行 求帮忙写下相关代码
...全文
354 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ajianchina 2015-07-24
  • 打赏
  • 举报
回复

getdata.ashx:  '{"Id":"1001","name":"zhangyu","score":"87"}'

$.get("getdata.ashx",function(data){
   var d = eval('('+data+')');
     alert(d['Id']);
     } );
我叫小菜菜 2015-07-24
  • 打赏
  • 举报
回复
//test.ashx

namespace ajax测试
{
    /// <summary>
    /// 摘要说明
    /// </summary>
    public class test : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
         context.Response.ContentType = "text/plain";
         string jsonstr=@"{Id = 1001,name= \"zhangyu\",score = 87}";
         context.Response.Write(jsonstr);
         //context.Response.Write("Hello World");
         //这里用Hello World代替了你要的json数据串{Id = 1001,name= "zhangyu",score = 87}
         }
        public bool IsReusable
        {
            get
            {
                return true;
            }
}
js调用:
function read_data() {
    var xmlhttp = new window.ActiveXObject("Microsoft.XMLHTTP");
    if (!xmlhttp) {
        alert("error:cannot create xmlhttp object.");
        return false;
    }
    xmlhttp.open("POST", "test.ashx", false);
    xmlhttp.onreadystatechange = function () {

        if (xmlhttp.readystate == 4) {
            if (xmlhttp.status == 200) //状态码200表示成功
            {
                //alert(xmlhttp.responseText);  //用于显示获取到的json格式化数据
                var data = eval('(' + xmlhttp.responseText + ')');
                //-----以下是通过getElementById函数修改内容----------
                document.getElementById("rc1_1").innerHTML = data.name;
                document.getElementById("rc19_6").innerHTML = data.id;

            }
            else {
                alert("error: status:" + xmlhttp.status.toString());
            }
        }
    }
    xmlhttp.send();
}
-------------------------- 以上是详细的实现,并没有经过封装。 使用jquery或者其他框架封装ajax的话,会简化很多,使用方便,如2楼。
binsweet 2015-07-24
  • 打赏
  • 举报
回复
var strJson = $.ajax({ url: "test.ashx”, async: false }).responseText;
风隐若现 2015-07-24
  • 打赏
  • 举报
回复
补充一下 用$.ajax 调用 谢谢
风隐若现 2015-07-24
  • 打赏
  • 举报
回复
引用 2 楼 binsweet 的回复:
var strJson = $.ajax({ url: "test.ashx”, async: false }).responseText;
怎么运用strJson数据

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧