Javascript ASP.NET MVC - 返回 JavaScriptResult 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7271005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC - Returning JavaScriptResult doesn't work
提问by User907863
If I try to return some JavaScript from my Controller like this:
如果我尝试像这样从我的控制器返回一些 JavaScript:
public ActionResult DoSomething()
{
return JavaScript("alert('Hello world!');");
}
I do not view the alert message in my browser, but I get a download request for a .js script from the page named as the action (register.js in my case). What is wrong?
我没有在浏览器中查看警报消息,但我从名为操作的页面(在我的情况下为 register.js)收到了一个 .js 脚本的下载请求。怎么了?
采纳答案by Sparkle
Try the below.
试试下面的。
public ActionResult DoSomething(){
return Json(new {isok=true, message="Your Message", data=...});
//True / False Json Return
//return UserObj == null ?
//Json(true, JsonRequestBehavior.AllowGet) :
//Json(string.Format("YourObject '{0}' to String", YourObject),
//JsonRequestBehavior.AllowGet);
}
//view
$.ajax
{
//code
success :function(returnvalue)
{
if(!returnvalue.isok)
{
window.alert("Error:" + returnvalue.message);
}
else
{
//do the stuff with returnvalue.data
}
}
回答by Carl Heinrich Hancke
I had a similar problem with the specified JavaScript not executing when returning the result as a JavaScriptResult
. In my case, the JavaScript content was rendering as text inside <pre>
tags.
我有一个类似的问题,指定的 JavaScript 在将结果作为JavaScriptResult
. 就我而言,JavaScript 内容在<pre>
标签内呈现为文本。
The solution is to return the JavaScript as a ContentResult
, by using the Content()
method. So try:
解决方案是ContentResult
使用Content()
方法将 JavaScript 作为,返回。所以尝试:
public ActionResult DoSomething()
{
return Content("<script language='javascript' type='text/javascript'>alert('Hello world!');</script>");
}
I found the answer on the ASP.NET forums. Have a look at Bruce's answer at the following link for a more complete explanation of why it gets done this way:
我在 ASP.NET 论坛上找到了答案。在以下链接中查看布鲁斯的回答,以更完整地解释为什么这样做:
回答by Martin
I would not return Javascript, I would return Content
, then on the page, I would convert that content to an alert:
我不会返回 Javascript,我会返回Content
,然后在页面上,我会将该内容转换为警报:
public ActionResult DoSomething()
{
return Content("Hello world!");
}
$.ajax({
url: "/Action/DoSomething/",
type: "POST",
success: editSuccess,
error: editFailure
});
function editSuccess(data) {
alert(data);
}
回答by Amir Chatrbahr
If you need to return a js code using JavaScriptResult, keep in mind that your action should be called via ajax call. Calling this action via Html.Actionlink or other controls directly, results in showing your script text instead of running the script. So you need to add a js code in your view:
如果您需要使用 JavaScriptResult 返回一个 js 代码,请记住您的操作应该通过 ajax 调用来调用。通过 Html.Actionlink 或其他控件直接调用此操作会导致显示脚本文本而不是运行脚本。所以你需要在你的视图中添加一段js代码:
<script type="text/javascript">
$(function () {
$('#myButtonId').click(function () {
$.getScript("/MyController/DoSomething");
});
});
</script>
and your action in MyController:
以及您在 MyController 中的操作:
public ActionResult DoSomething()
{
return JavaScript("alert('Hello world!');");
}
回答by Sylvia
When you are calling your action from the view, make sure you use @Ajax.ActionLink
instead of the @Html.ActionLink
helper method. The JavaScript will then render correctly and your alert will display.
当您从视图中调用您的操作时,请确保您使用@Ajax.ActionLink
而不是@Html.ActionLink
辅助方法。然后 JavaScript 将正确呈现并显示您的警报。