我正在尝试使用ajax和post方法,但它一直在弹出这个错误
尝试使用GET请求调用方法\ u0027SendEmail \ u0027,这是不允许的
这是js
var obj = { name: name, company: company, country: country, email: email, msg: Msg }
var json = JSON.stringify(obj);
$.ajax({
method: "POST",
url: "ContactUs.aspx/SendEmail",
contentType: "application/json; charset=UTF-8",
data: json,
dataType:"json",
success: function (data) {
var a = 3;
},
error:function(a,b){
var a = 43;
}
})
这是c#
上的服务器端[WebMethod]
public static void SendEmail(string name, string company, string country, string email, string msg)
{
}
提前致谢!
答案 0 :(得分:0)
尝试使用[HttpPost]代替[WebMethod]并删除静态。 它是REST API还是MVC?
答案 1 :(得分:0)
你应该这样:
您已使用[WebMethod]
修饰并添加using System.Web.Services;
引用的后端方法,我已更改方法以返回某些内容,以便验证其是否有效。
[WebMethod]
public static String sendEmail(string param)
{
return "Your String";
}
在您的.aspx
页面中,添加scriptManager
以启用客户端调用方法背后的代码
<asp:ScriptManager ID="scriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
最后在JQuery script
内,按照以下方式调用方法:
$(document).ready(function () {
PageMethods.sendEmail("", onSuccess);
function onSuccess(response)
{
alert(response);
}});
希望它有所帮助!