【问题标题】:ajax call in ascx Fileascx 文件中的 ajax 调用
【发布时间】:2013-08-16 10:18:45
【问题描述】:

我有一个 ascx 文件,我在其中对位于另一个文件中的函数(aspx 代码隐藏文件)进行 ajax 调用。但是它在结果中返回完整的 aspx 页面,我在函数中只返回字符串,下面是我的代码 这是在我的 ascx 文件中

  $.ajax({
            type: "POST",
            url: "MyFile.aspx/GetData", //url to point your webmethod          
            success: function (Result) {
                alert('success');
                $("#txtlicense").val(Result);
            },
            error: function () { alert('error'); }
        });

这是在 MyFile.aspx.cs 中

 [System.Web.Services.WebMethod()]
        public static string GetData()
        {
//Getting data from DB and returning 

        }

我也尝试将此方法放在我的 ascx.cs 文件中,但它给出错误

This type of page is not served

【问题讨论】:

    标签: jquery asp.net


    【解决方案1】:

    你不见了

    contentType: "application/json; charset=utf-8",
    dataType: "json",
    

    请参阅以下工作示例

    // 声明为静态方法的代码

    [WebMethod]
    public static string GetSquare(String value)
    {
        return value;
    }
    

    你的按钮,点击它必须完成

    <input type="button" id="button" value="Chnageurl" onclick="ajaxcall()" />
    

    脚本

    <script type="text/jscript">
    
    function ajaxcall(e) {
    
                $.ajax({
                type: "POST",
                url: "Default.aspx/GetSquare",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({ value: "Vinay" }),
                dataType: "json",
                success: function (value) {
                alert(value.d);
            },
           error: function () { alert("Ajax Error"); }
         });
      };
    

    【讨论】:

    • 非常感谢,我第二次错过了这个 contentType :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    相关资源
    最近更新 更多