【问题标题】:Javascript - Ajax call from code behindJavascript - 来自后面代码的 Ajax 调用
【发布时间】:2014-02-24 06:14:36
【问题描述】:

我需要从获取数据的代码隐藏中调用 Ajax javascript 函数。我正在使用以下方法来做同样的事情:

Page.ClientScript.RegisterStartupScript(
    GetType(), 
    "MyKey", 
    "Myfunction();", 
    true);

如果我在此处放置警报代替 MyFunction(),它可以工作,但是当我放置我的函数时,它就不起作用了:

   CsText.Append("var Announcements; ")

   CsText.Append("}}} ")
   CsText.Append(" $('.ui-checkbox').checkbox();")

如果我从 javascript 运行上述查询,它工作正常。有什么建议吗?? 是不是我们不能从代码隐藏中调用 ajax 方法?或者它在这个调用中使用 js 文件,所以它无法找到引用或者有任何其他方法可以做到这一点,比如我可以将所有这些放在前端代码中的函数中并从后面调用该方法本身 -使用 RegisterStartupScript 结束。请帮忙...

【问题讨论】:

    标签: javascript jquery asp.net ajax


    【解决方案1】:
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "MyKey", "Myfunction();", true);
    

    Page.ClientScript.RegisterStartupScript(this.GetType(), "MyKey", "Myfunction();", true);
    

    ClientScript.RegisterStartupScript(this.GetType(), "MyKey", "<script>Myfunction();</script>");
    

    【讨论】:

      【解决方案2】:

      如果您的代码在客户端正确呈现,Ajax 调用将起作用。但你写的不是正确的方法。我宁愿单独编写js代码并使用RegisterStartupScript方法调用它。

      Js 代码

        <script type="text/javascript">
         function Myfunction(){
             $.ajax({ type: "POST",
                  url: "SharepointListsAccess.asmx", cache: false
              });
         }
        </script>
      

      代码背后

      Page.ClientScript.RegisterStartupScript(
      GetType(), 
      "MyKey", 
      "Myfunction();", 
      true);
      

      【讨论】:

      • 好的,我试试看。
      • 如果我将其用作方法,则在执行时该方法的脚本会显示在屏幕上。像函数 Myfunction() { alert(1); } 这将显示在屏幕上。
      • 任何想法,为什么它显示脚本?
      • 我认为有些冲突。在全新页面中尝试此代码。
      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      • 2019-09-06
      • 2011-12-02
      • 2021-01-02
      • 1970-01-01
      相关资源
      最近更新 更多