我有简单的ajax:
$.ajax({
url: "http://localhost:50488/siteadmin3/search.aspx/TestJquery",
type: "post",
data: { id: '100', Name: 'Nilesh' },
success:function(result){
alert(result);
},
error: function (error) {
alert(error);
}
});
我的服务器端功能在search.aspx上:
[WebMethod()]
public static string TestJquery(string id, string Name)
{
return string.Format("Employee Id : {0} Name : {1} ", id, Name);
}
但是这个函数永远不会被调用。
可能是什么问题?
EDIT1:
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html
> xmlns="http://www.w3.org/1999/xhtml"><head><title> SA- Back
> office</title><link id='Link1' runat='server' rel='shortcut icon'
> href='images/favicon-pyle.ico' type='image/x-icon' /> <link id='Link2'
> runat='server' rel='icon' href='images/favicon-pyle.ico'
> type='image/favicon.ico' /> <style type="text/css"> <!--
> body,td { font-family: verdana, helvetica; font-size: 11px; color:
> #555555; } input.search { font-size: 9px; padding: 0px; margin: 0px; margin-top: 3px; } A:link { text-decoration: none; } A:visited {
> text-decoration: none; } A:hover { text-decoration: underline; }
> A.foot { color: #ffffff; } .carea { line-height: 16px; } .foot {
> font-size: 9px; }--></style> <script type="text/javascript"
> src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
> <script type="text/javascript"
> src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>
> <script type="text/javascript"> function CallJquery() {
> $.ajax({ url: "/siteadmin3/login.aspx/TestJquery",
> type: "post", data: JSON.stringify({ id: '100', Name:
> 'Nilesh' }) , success: function
> (result) { alert(result); },
> erro...rue, "", "", false, false))" id="test" />
> <script type="text/javascript">//<![CDATA[var Page_Validators = new
> Array(document.getElementById("RequiredFieldValidator1"),
> document.getElementById("RequiredFieldValidator2"));//]]></script><script
> type="text/javascript">//<![CDATA[var RequiredFieldValidator1 =
> document.all ? document.all["RequiredFieldValidator1"] :
> document.getElementById("RequiredFieldValidator1");RequiredFieldValidator1.controltovalidate
> = "adminName";RequiredFieldValidator1.errormessage = "User Required";RequiredFieldValidator1.evaluationfunction =
> "RequiredFieldValidatorEvaluateIsValid";RequiredFieldValidator1.initialvalue
> = "";var RequiredFieldValidator2 = document.all ? document.all["RequiredFieldValidator2"] :
> document.getElementById("RequiredFieldValidator2");RequiredFieldValidator2.controltovalidate
> = "adminpassword";RequiredFieldValidator2.errormessage = "Password Required";RequiredFieldValidator2.evaluationfunction =
> "RequiredFieldValidatorEvaluateIsValid";RequiredFieldValidator2.initialvalue
> = "";//]]></script><script type="text/javascript">//<![CDATA[var Page_ValidationActive = false;if (typeof(ValidatorOnLoad) ==
> "function") { ValidatorOnLoad();}function ValidatorOnSubmit() {
> if (Page_ValidationActive) { return ValidatorCommonOnSubmit();
> } else { return true; }}
> //]]></script></form></body></html>
答案 0 :(得分:2)
首先,您需要在脚本管理器中启用页面方法,如下所示:
<asp:ScriptManager runat="server" EnablePageMethods="true">
您还需要设置内容类型和数据类型:
您的内容类型必须是:
contentType: "application/json; charset=utf-8"
您的数据类型必须是:
dataType: "json"
其中如下:
$.ajax({
url: "http://localhost:50488/siteadmin3/search.aspx/TestJquery",
type: "post",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { id: '100', Name: 'Nilesh' },
success:function(result){
alert(result);
},
error: function (error) {
alert(error);
}
});
答案 1 :(得分:1)
我的方法是:在您的方法之前放置[HttpPost]属性。就像这样:
[WebMethod()]
[HttpPost]
public static string TestJquery(string id, string Name)
{
return string.Format("Employee Id : {0} Name : {1} ", id, Name);
}
答案 2 :(得分:1)
不要对您的网址进行硬编码。你想瞄准的目标是:
$.ajax({
url: "/search/TestJquery",
type: "post",
data: { id: '100', Name: 'Nilesh' },
success:function(result){
alert(result);
},
error: function (error) {
alert(error);
}
});
并且当然将您的服务器端功能声明为:
[HttpPost]
public static string TestJquery(string id, string Name)
{
return string.Format("Employee Id : {0} Name : {1} ", id, Name);
}