使用 Javascript 未在视图中调用控制器操作
Controller action is not called in view using Javascript
我对 MVC 非常陌生。我有 javascript 函数读取 xml 字符串并显示各种值。 XML 中的元素之一是加密形式。我已经在控制器中编写了将 return 解密字符串的方法,但我无法使用 ajax 在我的 javascript 方法中调用该方法。下面是控制器方法的代码和javascript代码。
[HttpPost]
public ActionResult DecryptMessage(string message)
{
string decryptedMessage= Encryption.DecryptData(message);
return Json(decryptedMessage);
}
部分Javascript代码
if ((x[i].nodeName).toUpperCase() == "MESSAGE" ) {
//alert(childnd.nodeValue);
$.ajax({
type: "POST",
data: { value: childnd.nodeValue },
url: "/QueryController/DecryptMessage",
dataType:"string",
Success: function (result) {
if (result > 0) {
stringDetails = stringDetails + '<br><strong>Value:</strong>' + result + '<br>';
}
})
}
我不知道哪里出了问题。
由于这是您正在处理的 URL:localhost/LogViewerPortal/Query/Index
,您的 AJAX 应该触发相同的 URL。另外,将 Success
更改为 success
:
if ((x[i].nodeName).toUpperCase() == "MESSAGE" ) {
//alert(childnd.nodeValue);
$.ajax({
type: "POST",
data: { message : childnd.nodeValue },
url: "/LogViewerPortal/Query/DecryptMessage",
success: function (result) {
if (result) {
stringDetails = stringDetails + '<br><strong>Value:</strong>' + result + '<br>';
}
})
}
如果您想 return 来自控制器的字符串,您可以更改为:
[HttpPost]
public ActionResult DecryptMessage(string message)
{
string decryptedMessage= Encryption.DecryptData(message);
return Content(decryptedMessage);
}
我对 MVC 非常陌生。我有 javascript 函数读取 xml 字符串并显示各种值。 XML 中的元素之一是加密形式。我已经在控制器中编写了将 return 解密字符串的方法,但我无法使用 ajax 在我的 javascript 方法中调用该方法。下面是控制器方法的代码和javascript代码。
[HttpPost]
public ActionResult DecryptMessage(string message)
{
string decryptedMessage= Encryption.DecryptData(message);
return Json(decryptedMessage);
}
部分Javascript代码
if ((x[i].nodeName).toUpperCase() == "MESSAGE" ) {
//alert(childnd.nodeValue);
$.ajax({
type: "POST",
data: { value: childnd.nodeValue },
url: "/QueryController/DecryptMessage",
dataType:"string",
Success: function (result) {
if (result > 0) {
stringDetails = stringDetails + '<br><strong>Value:</strong>' + result + '<br>';
}
})
}
我不知道哪里出了问题。
由于这是您正在处理的 URL:localhost/LogViewerPortal/Query/Index
,您的 AJAX 应该触发相同的 URL。另外,将 Success
更改为 success
:
if ((x[i].nodeName).toUpperCase() == "MESSAGE" ) {
//alert(childnd.nodeValue);
$.ajax({
type: "POST",
data: { message : childnd.nodeValue },
url: "/LogViewerPortal/Query/DecryptMessage",
success: function (result) {
if (result) {
stringDetails = stringDetails + '<br><strong>Value:</strong>' + result + '<br>';
}
})
}
如果您想 return 来自控制器的字符串,您可以更改为:
[HttpPost]
public ActionResult DecryptMessage(string message)
{
string decryptedMessage= Encryption.DecryptData(message);
return Content(decryptedMessage);
}