【发布时间】: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