加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET MVC – 返回JavaScriptResult不起作用

发布时间:2020-12-16 04:00:29 所属栏目:asp.Net 来源:网络整理
导读:如果我尝试从我的Controller返回一些 JavaScript,如下所示: public ActionResult DoSomething(){ return JavaScript("alert('Hello world!');"); } 我没有在我的浏览器中查看警报消息,但是我从名为action的页面获取了.js脚本的下载请求(在我的情况下是regis
如果我尝试从我的Controller返回一些 JavaScript,如下所示:
public ActionResult DoSomething()
{       
    return JavaScript("alert('Hello world!');");            
}

我没有在我的浏览器中查看警报消息,但是我从名为action的页面获取了.js脚本的下载请求(在我的情况下是register.js).怎么了?

解决方法

试试下面的内容.
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
}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读