ajax调用后台方法,求大神

写了一段ajax的代码,原意是打算,当选择句子时,用ajax将选中的句子传回后台,当ajax回传成功时,执行后台方法BTCompare。但是现在页面加载的时候直接执行了BTCompare,反而点击按钮触发ajax的时候不执行

        //选取句子,点击按钮,传送回后台
        function ajaxSend() {
            $('#btnSend').attr('disabled', true)
            $.ajax({
                type: "Post",
                url: "Reader.aspx/GetStr",
                async: false,
                data:"{'s':'"+selectedText+"'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                     alert(data.d);
                    if (data.d)
                    {
                        function Init() {
                            var s = '<%=BtCompare() %>';
                        }
                    }
                },
                error: function (err) {
                    alert("data error");
                }
            });
        }
        document.onmousedown = function () { selectedText = false; }
        document.onmouseup = function (e) {
            e = e || window.event;
            selectedText = window.getSelection ? window.getSelection().toString() : document.selection ? document.selection.createRange().text : false;
            if (selectedText) {
                //判断页面是否有滚动,有的话还得加上滚动的距离,要不按钮定位不准
                var sl = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft),
                st = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
                $('#btnSend').css({ left: e.clientX + sl, top: e.clientY + st }).show().attr('disabled', false);
            }
        }
 var s = '<%=BtCompare() %>';

是服务器端的代码,肯定是执行完毕了才会向客户端发送你的值。

你这个要求另外发送一个ajax请求就好了,或者将你的BtCompare放到Reader.aspx/GetStr这个方法里面调用,然后输出你内容就好了,通过回调函数的参数data可以得到你的BtCompare返回值

注意GetStr是通过Response.Write输出或者直接return 返回BtCompare的值,不能设置服务器控件的内容,这2个是不同的请求,不会反映到你的html页面

 [WebMethod]
    public static string GetStr(string s)
    {
        return BtCompare(s);//////////////////////
    }

    private string BtCompare(string s)////////参加参数,是从客户端发送的选择的网页内容,自己修改参数
    {
        //在页面选中一段句子,在某个地方弹出一个按钮,点击按钮,将选中的句子传入到下面的s当中,然后调用方法,将两个东西传入到o.txt和s.txt,然后比较,接着切割句子,最后输出
        //string s = "As a linguist, he acknowledges that all varieties of human language, including non-standard ones like Black English, can be powerfully expressive--there exists no language or dialect in the world that cannot convey complex ideas.";//在前台选中语句,传入到这里

        demo gg = new demo();
        string[] sentence = gg.SentenceDetect(linkWord);
        string otxt = gg.Parse(sentence);//放在o.txt
        FileStream fotxt = new FileStream(@"D:\\parser\\o.txt", FileMode.Create);
        StreamWriter ot = new StreamWriter(fotxt);
        ot.Write(otxt);
        ot.Flush(); //清除缓存
        ot.Close();
        ot.Dispose();
        fotxt.Close();

        DoParser doq = new DoParser();
        string stxt = doq.parser(linkWord);//放在s.txt
        FileStream fstxt = new FileStream(@"D:\\parser\\s.txt", FileMode.Create);
        StreamWriter st = new StreamWriter(fstxt);
        st.Write(stxt);
        st.Flush();
        st.Close();
        st.Dispose();
        fstxt.Close();

        Process p = new Process();
        p.StartInfo.FileName = @"cmd.exe";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.CreateNoWindow = true;
        p.Start();

        string str = "D:\\BCompare\\BCompare.exe @D:\\parser\\BCconsle.txt D:\\parser\\o.txt D:\\parser\\s.txt D:\\学习档案\\Reading\\Reading\\report.html ";//保存到当前目录下
        p.StandardInput.WriteLine(str);
        p.StandardInput.WriteLine();
        p.StandardInput.AutoFlush = true;
        p.StandardInput.WriteLine("exit");
        p.StandardOutput.ReadToEnd();
        p.Close();
        Report.Text = "<iframe src='report.html'frameborder='0' height=350px width=100%></iframe>";

        Utils uu = new Utils();
        //String[] ss = uu.subSentenceASC(s);//先长后短句
        String[] ss = uu.subSentenceDESC(stxt);//先短后长句
        int count = 0;
                string rst="";///////////
        foreach (string aa in ss)
        {
             rst += ++count + "、" + aa + "<br />";///////////////////
        }
                return rst;
    }



success: function (data) {
                     alert(data.d);
                    if (data.d)
                    {////////将BtCompare得到的返回值设置到容器里面

                                        $('#xxxxxxxxxxxxx').html(d.data);//注意修改选择器
                    }
                },