Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

c# - Call javascript function from asmx

I want to return javascript function from asmx as string like following..

All html tags return but checkNewMsg variant 'script tag' doesnt return! What happens really ?

Please advice

 <script type="text/javascript">
    function getWindow(FromUserID, UserID, PerID, UserName) {
        $.ajax({
            type: "POST",
            url: "TestMessageService.asmx/OpenWindow",
            data: "{'FromUserID': '" + FromUserID + "', 'ClickedUserID': '" + UserID + "', 'ClickedPerID': '" + PerID + "', 'ClickedUserName': '" + UserName + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                var msgs = response.d;

                $('#div_Panel').append(msgs).fadeIn("slow");

                var elements = $('.panelContent');
                for (var i = 0; i < elements.length; i++) {
                    elements[i].scrollTop = elements[i].scrollHeight;
                }
            },
            failure: function (msg) {
                $('#div_Panel').text(msg);
            }
        });
    }
</script>

[WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
public string OpenWindow(string FromUserID, string ClickedUserID, string ClickedPerID, string ClickedUserName)
{ 
    string checkNewMsg = "<script type="text/javascript">window.setInterval(fc_" + ClickedUserName.Replace(" ", "") + ", 10000); function fc_" + ClickedUserName.Replace(" ", "") + "() { alert('" + ClickedUserName + "'); }</script>";

    StringBuilder sb = new StringBuilder();
    sb.Append(checkNewMsg + "<div class="ch_Box">");
    sb.Append("<div class="ch_Header">");
    sb.Append("<div style="float:left;margin-top: 9px;margin-left: 8px;"><img src="Images/Status.png"></div>");
    sb.Append("<div id="roomUsers" class="ch_HeaderItem">" + ClickedUserName + "</div>");
    sb.Append("<div onclick="closePanel(this)" style="width: 23px; height: 27px; cursor: pointer; position: absolute; margin-left: 232px;"><img style="height: 20px; margin-top: 4px;" src="Images/close.png"></div>");
    sb.Append("<div id="cont_" + ClickedUserID + "" class="panelContent">" + FillMessages(roomID, FromUserID.ToInt()) + "</div>");
    sb.Append("<div class="ch_Text">");
    sb.Append("<input id="msg_" + FromUserID + "_" + ClickedUserID + "_" + ClickedPerID + "_" + roomID + "" type="text" class="inp" onkeypress="PushText(this)" autocomplete="off" /></div>");
    sb.Append("</div></div>");

    return sb.ToString();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't know why doesn't return script tag an asmx but when i remove the tag and then in the js side while i add script' tag before return value the problem is solved.

Just like this;

In asmx side;

string checkNewMsg = "window.setInterval(fc_" + ClickedUserName.Replace(" ", "") + ", 10000); function fc_" + ClickedUserName.Replace(" ", "") + "() { alert('" + ClickedUserName + "'); }#func#";

In Js Side;

 success: function (response) {
                var msgs = response.d;
                var arrCont = msgs.split('#func#');
                var MsgCont = "<script type="text/javascript">" + arrCont[0] + "</script>";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...