首页 新闻 会员 周边

请教一个ajax问题!!

0
悬赏园豆:10 [已解决问题] 解决于 2011-11-25 16:08

<body>
    <form id="form1" runat="server">
        <textarea id="inputTxt" name="inputTxt" style=" width:200px; height:200px;"></textarea>
        <input type="button" value="发布" id="publishBtn" />
    </form>
</body>
</html>
<script type="text/javascript">
    $(function () {
        $("#publishBtn").click(function () {
            var content = $("#inputTxt").val();
            $.post("/Modules/ajax.aspx?action=aa", { content: encodeURIComponent(content) }, function (res) {
                alert(res);
            })
        })
    })
</script>

方法这样写的:

public string aa()
        {
                        return "123";
        }

为什么alert出得是

luckydd的主页 luckydd | 初学一级 | 园豆:77
提问于:2011-11-25 13:54
< >
分享
最佳答案
0

ajax.aspx页面前台除了<%@ Page 指令外其它代码要清空

收获园豆:5
cniteeq | 菜鸟二级 |园豆:210 | 2011-11-25 14:40

这样是对了,可怎么弹不出内容呢,总是空

public string aa()
        {
            string content = Request.QueryString["content"].ToString();
                        return content;
        }

后台是上面这样写的,如果是这样接值写错了,直接ruturn "111",这样也是空

luckydd | 园豆:77 (初学一级) | 2011-11-25 14:51

@blog_doudou: 

先在Page_Load事件里面写Response.Write("aaaaa");看能接到吗,能接到的话,改成Response.Write(Request["content"]);看能不能接到,Post提交的值用Response.From[""] 或Response[""]接值

cniteeq | 园豆:210 (菜鸟二级) | 2011-11-25 14:59

或Request[""]接值写错了

cniteeq | 园豆:210 (菜鸟二级) | 2011-11-25 15:01

如果接不到值改下前台

 

$.ajax({ type: "POST", url: "/Modules/ajax.aspx", data: "content=" + content + "", success: function (res) {  alert(res)     }             });

cniteeq | 园豆:210 (菜鸟二级) | 2011-11-25 15:11

@cniteeq: 对了,就是接值写错了,

public partial class ajax : System.Web.UI.Page
    {
        public string action = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            action=Request["action"].ToString();
            switch (action)
            {
                case "aa":
                    Response.Write(aa());
                    break;
            }
        }

        public string aa()
        {
            string content = Request["content"].ToString();
            return content;
        }
    }

改成这样的就对了,谢谢了~~

luckydd | 园豆:77 (初学一级) | 2011-11-25 15:52
其他回答(3)
0

/Modules/ajax.aspx?action=aa

应该是这里的问题,你既然用了post为什么还要这样传参数呢?你用firebug查看一下吧

收获园豆:2
artwl | 园豆:16736 (专家六级) | 2011-11-25 14:09

/Modules/ajax.aspx?action=aa,action后面是方法名呀,{ content: encodeURIComponent(content) }这块是参数,用firebug查不出来~~,请教这里该怎么写呀

支持(0) 反对(0) luckydd | 园豆:77 (初学一级) | 2011-11-25 14:36

@blog_doudou: 你传递一个方法名过去要用反射动态调用才行的,最好改为webservice

支持(0) 反对(0) artwl | 园豆:16736 (专家六级) | 2011-11-25 14:47

@artwl: 有这么复杂呀~~,看人家的代码是这样子的呀,为什么到我这就不对了呢

支持(0) 反对(0) luckydd | 园豆:77 (初学一级) | 2011-11-25 14:55

@blog_doudou: 

一般都不把方法名作参数传递的,关键要把ajax原理弄明白,用webservice的话参考:

http://www.cnblogs.com/terryfeng/archive/2009/02/01/1382123.html

支持(0) 反对(0) artwl | 园豆:16736 (专家六级) | 2011-11-25 14:58

@artwl: 恩,谢谢了

支持(0) 反对(0) luckydd | 园豆:77 (初学一级) | 2011-11-25 15:54
0

很想知道你后台怎么写的?为什么不用.ashx?

收获园豆:1
写代码的小2B | 园豆:4371 (老鸟四级) | 2011-11-25 14:44

public string aa()
        {
            string content = Request.QueryString["content"].ToString();
            return content;
        }

我后台是这样写的,.ashx用这个怎么写呀,求示例

支持(0) 反对(0) luckydd | 园豆:77 (初学一级) | 2011-11-25 14:53

@blog_doudou: 如果你一定要用aspx呢你这样写是不对的,

Response.Write(content); 还有这些代码应该写到Page_Load里面,

ashx雷同,你可以自己去谷歌。都说出来就没有意思了,很基础的东西。

支持(0) 反对(0) 写代码的小2B | 园豆:4371 (老鸟四级) | 2011-11-25 14:58

@写代码的小2B: 哦,谢了

支持(0) 反对(0) luckydd | 园豆:77 (初学一级) | 2011-11-25 15:56
0

我也是刚刚学习jquery,才几天,不过基本的都差不多了解了,你要ashx我给你一个参考的

 1  public void ProcessRequest(HttpContext context)
2 {
3 context.Response.ContentType = "text/plain";
4 string type = context.Server.UrlDecode(context.Request.QueryString["type"]);
5 if (!string.IsNullOrEmpty(type))
6 context.Response.Write(GetHtml(type));
7 }
8
9 public bool IsReusable
10 {
11 get
12 {
13 return false;
14 }
15 }
16
17 ///<summary>
18 /// 建立数据库绑定语句
19 ///</summary>
20 ///<param name="type"></param>
21 ///<returns></returns>
22 public string GetHtml(string type)
23 {
24 StringBuilder sb = new StringBuilder();
25 SqlData da = new SqlData();
26 string sql = "select * from tbl_News where Categories='" + type + "' order by IssueDate desc";
27 DataSet ds = da.ExecuteDataSet(sql);
28 if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
29 {
30 foreach (DataRow row in ds.Tables[0].Rows)
31 {
32 sb.Append("<tr><td>" + row["Title"].ToString() + "</td><td>" + row["IssueDate"].ToString() + "</td></tr>");
33 }
34 }
35 return sb.ToString();
36 }
37 }
收获园豆:2
穆哈咖啡 | 园豆:198 (初学一级) | 2011-11-25 15:38

谢了

支持(0) 反对(0) luckydd | 园豆:77 (初学一级) | 2011-11-25 15:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册