【问题标题】:Ajax call returning whole page html in asp.net web formsAjax 调用在 asp.net Web 表单中返回整个页面 html
【发布时间】:2017-01-13 09:55:25
【问题描述】:

我正在开发 asp.net 网络表单应用程序。我试图在代码隐藏中对 web 方法进行 ajax 调用,但它不是返回结果,而是返回整个 html 页面。

我在点击按钮时调用它

 <input type="button" id="btnCallAPIFromClient" class="btn btn-success" value="Call API from Client"/>

我的js脚本是:

$(document).ready(function() {
    //PageMethods.set_path(PageMethods.get_path() + '.aspx');
    $('#btnCallAPIFromClient').click(function() {
        alert('here');
        $.ajax({
            url: '/login/GetAccessToken',
            type: "POST",
            dataType: 'html',
            success: function(response) {
                alert(response);
                debugger;
                sessionStorage.setItem("accessToken", response.access_token);
                alert(response.access_token);
            },
            // Display errors if any in the Bootstrap alert <div>
            error: function(jqXHR) {
                alert(jqXHR.responseText);
            }
        });
    });
});

网络方法是:

   [WebMethod]
        public static string GetAccessToken()
        {
           return "abc";
        }

【问题讨论】:

  • 通知dataType: 'html',尝试一次dataType: "text",
  • 另加contentType: "application/json; charset=utf-8",dataType: "json"
  • 对于真正确定 ajax 结构以正确方式形成的人可能会怀疑 ScriptModule 不能很好地加载。我有答案here

标签: javascript jquery asp.net ajax


【解决方案1】:

这是因为您已将数据类型设置为 html。 将数据类型更改为文本,如下所示:

dataType: 'text'

并且也在成功函数中改变这一行

sessionStorage.setItem("accessToken", response.access_token);

sessionStorage.setItem("accessToken", response);

因为您没有收到 json 格式的数据。所以这将是无效的。

【讨论】:

  • @AsifHameed,你试过我在评论中提到的吗?
  • 是的,我在 chrome 的网络选项卡中看到了这个:{Message: "Authentication failed.", StackTrace: null,...} ExceptionType: "System.InvalidOperationException" Message: "Authentication failed."堆栈跟踪:空
  • 请看看 ---> link。也许这就是你正在苦苦挣扎的地方
  • 感谢 Prashant Pokhriyal
猜你喜欢
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-21
  • 2021-07-26
  • 1970-01-01
  • 2014-06-19
  • 2010-11-11
相关资源
最近更新 更多