我最近将我的JS函数从一个母版页移到一个单独的.js文件中。由于这样做,ajax函数不会调用代码隐藏的webmethod。
通过OnClientClick调用该函数,并且存在键/对数据值。但是,ajax不会从此外部.js文件调用webmethod。我尝试过不同形式的网址而没有成功。
任何人都可以向我解释为什么ajax函数不会调用代码隐藏的webmethod?
这只是Micorsoft的“功能”吗?
<script src="/JavaScript/TreasurersChecksEE/TCEE.js" type="text/javascript"></script>
<asp:Button class="btn btn-primary btn-sm" ID="refundSearch" OnClientClick="refNum('txtRefNo'); saveDdlParms();" OnClick="btnRefundSearch_Click" TabIndex="0" runat="server" Text="Search" type="button" UseSubmitBehavior="false"/>
[WebMethod(EnableSession = true)]
public static void SaveMyParameters(string ddlValue, string ddlText, string refund)
{
string refundNum = refund;
string issueTypeVal = ddlValue;
string issueTypeTxt = ddlText;
HttpContext.Current.Session["Refund Number"] = refundNum;
HttpContext.Current.Session["Issue Type Value"] = issueTypeVal;
HttpContext.Current.Session["Issue Type Text"] = issueTypeTxt;
}
function saveDdlParms() {
$.ajax({
url: "/TreasurersChecksEE.aspx/SaveMyParameters",
type: "POST",
contentType: "application/json",
data: JSON.stringify({ ddlValue: issueTypeVal, ddlText: issueTypeTxt, refund: refundNo }),
context: document.body
// ,success: function (response) {
// response ? alert("No: " + num + " Txt: " + txt + " RefNo: " + refundNo ) : alert("It didn't work.");
// }
})
};