【发布时间】:2013-01-07 14:50:18
【问题描述】:
我正在尝试在 .net 中进行简单的 ajax 调用
有什么建议吗?
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
我在浏览器中这样调用 webmethod: http://localhost.com/Ajax/WebService1.asmx/HelloWorld
这会导致
“找不到资源。”
可能是因为 url 语法。
我的路线设置如下:
routes.IgnoreRoute("Ajax/");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
如果我删除MapRoute,网络电话可以正常工作,但我网站的其余部分会失败。
有什么建议吗?
更新: 我改为使用控制器。当我在浏览器中使用 url 调用它时,我在控制器中点击了断点。但不是当我运行这段代码时:
<div id="Result">
Kig her!
</div>
@section javascript {
$(function () {
$("#FirstReminder").datepicker();
$("#EndDate").datepicker();
});
$(document).ready(function() {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function() {
alert('kkk');
$.ajax({
type: "POST",
url: "AjaxWorkflow/GetSteps",
data: {workflowId: "1", workflowStep: "test"},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
});
});
更新 2: 我通过将行更改为来解决它
data: "{workflowId: '1', workflowStep: 'test'}",
【问题讨论】: