helloxielan 2014-08-04 12:25 采纳率: 0%
浏览 43

ajax参数始终为null?

I am trying to pass the a string to my behind code, but its always null.

$.ajax({
   type: "POST",
   url: "Search.aspx?do=SearchText",
   data: {searchText: searchText },
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   success: fnsuccesscallback,
   error: fnerrorcallback
});



protected void Page_Load(object sender, EventArgs e)
 {
    if (Request["do"] != null && Request["do"] == "SearchText")
    {
       string result = Search.searchText(Request["searchText"]);
    }
 }

My Request["searchText"] is always null.

EDIT:


public void DoSearch(string SearchText)
{
    string result = Search.searchText(SearchText);
}

and in the ajax post:

 url: "Search.aspx/DoSearch",
 data: { searchText: searchText },
  • 写回答

3条回答 默认 最新

  • weixin_33720956 2014-08-04 12:44
    关注

    The problem with this is you are trying to grab the querystring within the Page_Load event which fires before the ajax code. So on Page_Load there is no parameter passed at that stage which is why it will always be null.

    评论
  • weixin_33737134 2014-08-04 13:36
    关注

    Try calling it as a static webmethod, passing the parameter directly into the called method.

           url: "Search.aspx/DoSearch",
           data: '{"searchText":"' + searchText + '"}',
    
    
    
    
            //Add this on code behind page...
            using System.Web.Services;
    
    
    
            [WebMethod]
            public static void DoSearch(String searchText)
            {
            //Do your stuff!!
            }
    
    评论
  • weixin_33739523 2014-08-04 14:35
    关注

    Ok I have set up a basic test which works for me.

                    var myString = 'Test';
    
                    $.ajax({
                        type: "POST",
                        url: "SearchTest.aspx/DoSearch",
                        data: '{"searchText":"' + myString + '"}',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: fnsuccesscallback,
                        error: fnerrorcallback
                    });
    
    
            function fnsuccesscallback() {
    
            }
    
            function fnerrorcallback() {
    
            } 
    

    //Remember the webmethod goes into the code behind of the SearchTest.aspx page

            [WebMethod]
            public static void DoSearch(String searchText)
            {
                //Do your stuff!!
            }
    

    Remember to import the namespace...using System.Web.Services;

    Hope this works for you

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab处理脑电数据悬赏(时序图+预处理+频谱图)
  • ¥100 r语言多元回归模型怎么看表达式
  • ¥15 PMM 9010/30P
  • ¥15 pom文件依赖管理,未找到依赖
  • ¥15 现在后端返回给我一堆下载地址,都是一个视频切片后的,如何在uniapp安卓环境下将这些分片的视频下周并且合并成原始视频并下载到本地。
  • ¥15 Unity导出2D项目运行时图片变成马赛克
  • ¥15 关于communitytoolkit.mvvm的生成器得到的代码看起来没有被使用的问题
  • ¥15 matlab中此类型的变量不支持使用点进行索引
  • ¥15 咨询第六届工业互联网数据创新大赛原始数据
  • ¥15 Pycharm无法自动补全,识别第三方库函数接收的参数!