[英]Ajax call not hitting code behind c# method
I have a jQuery ajax call我有一个 jQuery ajax 电话
<script type="text/javascript">
$(document).ready(function () {
$('#MainContent_minuteBooks').click(function (e) {
e.preventDefault();
$.ajax({
type: "GET",
url: "MainView.aspx/GetPageTypes",
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
},
success: function (response) {
console.log("--" + JSON.stringify(response));
}
});
});
});
</script>
This should call my code behind WebMethod
这应该在
WebMethod
后面调用我的代码
[WebMethod]
public static string GetPageTypes()
{
List<string> pages = new List<string>();
string html = "";
Console.WriteLine(book.Count);
foreach(MinuteBookPage mbp in book)
{
if (!pages.Contains(mbp.Class)) { pages.Add(mbp.Class); };
}
foreach(string s in pages)
{
html += "<div class='page' style='border: solid 2px red'><p>" + s + "</p></div>";
}
Console.WriteLine(html);
return html;
}
I have breakpoints set on the method, as well as a Console.WriteLine
that should be display, the breakpoints are not hit, and the console does not output anything我在方法上设置了断点,以及应该显示的
Console.WriteLine
,没有命中断点,控制台没有 output 任何东西
I am lead to believe that my method is not being called我被引导相信我的方法没有被调用
In the network tab this is displayed, First call , there is a second call after this 301 response, and it just returns the page html/javascript在网络选项卡中显示,第一次调用,在这个 301 响应之后有第二次调用,它只是返回页面 html/javascript
Upon success the ajax call returns the html and JavaScript markup for the page i am on成功后,ajax 调用返回我所在页面的 html 和 JavaScript 标记
I have looked at this link Pagemethods in asp.net but it seems outdated, and all other resources ive looked at dont seem to follow what it outlines我已经查看了asp.net 中的这个链接 Pagemethods但它似乎已经过时了,我查看的所有其他资源似乎都没有遵循它的概述
Thanks to @Dortimer for pointing me to this post, and helping me work out the issue.感谢@Dortimer 将我指向这篇文章,并帮助我解决了这个问题。
How to properly make an ajax call with webforms 如何使用网络表单正确拨打 ajax
I ended up with the following code我最终得到了以下代码
Ajax call
<script type="text/javascript">
$(document).ready(function () {
$('#MainContent_minuteBooks').click(function () {
console.log("inside click");
$.ajax({
type: 'POST',
url: '<%= ResolveUrl("~/MainView.aspx/GetPageTypes") %>',
data: '{ }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
console.log(msg)
}
});
return false;
});
});
</script>
Also i had to turn off RedirectMode inside of the RouteConfig.cs
file我还必须在
RouteConfig.cs
文件中关闭 RedirectMode
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Off;
routes.EnableFriendlyUrls(settings);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.