[英]Dotnetnuke - How to call method in code behind with Ajax
This is script 这是剧本
<script type="text/jscript">
function ajaxcall(e) {
$.ajax({
type: "POST",
url: "/DesktopModules/Modules/Admin/City/AddCity.ascx/GetMethod",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ value: "Vinay" }),
dataType: "json",
success: function (value) {
alert(value.d);
},
error: function () { alert("Ajax Error"); }
});
};
This is code behind: 这是背后的代码:
[WebMethod]
public static string GetMethod(String value)
{
return value;
}
When i call function from button. 当我从按钮调用功能。 It show js alert
Ajax Error
它显示js警报
Ajax Error
<input type="button" id="button" value="Test" onclick="ajaxcall()" />
I was tried replace /DesktopModules/Modules/Admin/City/AddCity.ascx/GetMethod
by AddCity.ascx/GetMethod
but it still not work ! 我试图用
/DesktopModules/Modules/Admin/City/AddCity.ascx/GetMethod
AddCity.ascx/GetMethod
替换/DesktopModules/Modules/Admin/City/AddCity.ascx/GetMethod
,但是它仍然无法正常工作!
You can't call a WebMethod from an ASCX usercontrol - IIS won't allow it. 您不能从ASCX用户控件调用WebMethod-IIS不允许。 It must be in an ASPX page.
它必须在ASPX页面中。
If you don't need any security, you can create a generic handler (.ASHX file). 如果不需要任何安全性,则可以创建通用处理程序(.ASHX文件)。
public class CityHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var fn = context.Request.QueryString["action"];
var newCity = context.Request.QueryString["city"];
if (fn == "add")
{
// TODO: add city
}
context.Response.ContentType = "text/plain";
context.Response.Write("OK");
}
public bool IsReusable
{
get { return false; }
}
}
Then change your ajax code: 然后更改您的ajax代码:
$.ajax({
type: "GET",
url: "/DesktopModules/Modules/Admin/City/CityHandler.ashx?action=add&city=Vinay",
success: function (value) {
alert(value);
},
error: function () { alert("Ajax Error"); }
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.