首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

一个有关问题,jquery+ajax,往httphandlers(也就是后台)传递的 data: {"school" :"南京大学

2012-03-20 
请教大家一个问题,jquery+ajax,往httphandlers(也就是后台)传递的 data: {school :南京大学} 参数,后

请教大家一个问题,jquery+ajax,往httphandlers(也就是后台)传递的 data: {"school" :"南京大学"} 参数,后台怎样才能接
请教大家一个问题,jquery+ajax,往httphandlers(也就是后台)传递的 data: {"school" :"南京大学"} 参数,后台怎样才能接到呢?

注意:数据传送方式为post (get的话,很简单就取到了。)

1,web侧的代码:

HTML code
    $(function(){          $.ajax ({      url : "MyJsonHandler/student.json",  // JSON数据的地址            type: 'post',                        // 数据传送方式                          dataType: 'json',                    // 数据类型            data: {"school" :"南京大学"},                    contentType: 'application/json',        success : function( data, textStatus,jqXHR ) {         alert("success");        },        // 返回结果为失败        error : function(jqXHR, textStatus, errorThrown) {        alert('error');            }        });        });      </script>  


2,server侧的代码(c#完成)
C# code
using System;using System.Collections.Generic;using System.Text;using System.Web;namespace MyHandlers{    public class MyJsonHandler : IHttpHandler    {        public bool IsReusable        {            get { return true; }        }        public void ProcessRequest(HttpContext context)        {            // 如何取得 "school" 对应的值是 "南京大学" ????        }    }}



我的问题:

如何从HttpContext 里面取到我在web侧设置的ajax的data {"school" :"南京大学"} 呀?[b][/b]

如果数据传送方式为get的话,利用
 context.Request.QueryString["school"]
就可以取到 “南京大学”了。


[解决办法]

试试这个,我是这样取值的。

string school= context.Request.Params["school"].ToString();
[解决办法]
url: "Test/Handler.ashx?school=1111", 

那只能在URL后面加参数了。
[解决办法]
dataType: 'json',
contentType: 'application/json',

这两句去掉。我post的时候 加上这两句就不成功,去掉就可以了
[解决办法]
方法里头加个school的参数不就完了。

热点排行