关于json result的实例代码

public jsonresult jsondata()
        {
            httpcontext.response.appendheader(“access-control-allow-origin”, “*”);
          
          
            return json(db.weathers.tolist());
        }

json方法有一个重构:

protected internal JsonResult Json(object data); protected internal JsonResult Json(object data, JsonRequestBehavior behavior);

我们只需要使用第二种就行了,加上一个 json请求行为为Get方式就OK了

public JsonResult GetPersonInfo() { var person = new { Name = “张三”, Age = 22, Sex = “男” }; return Json(person,JsonRequestBehavior.AllowGet); }

 

这样一来我们在前端就可以使用Get方式请求了:

 

view

$.ajax({ url: “/FriendLink/GetPersonInfo”, type: “POST”, dataType: “json“, data: { }, success: function(data) { $(“#friendContent”).html(data.Name); } })

 

Index2var login = function () {            $.ajax({ type: "post", url: "http://localhost:4968/Weathers/JsonData", data: null, success: function (res) {                alert(JSON.stringify(res));            }, dataType: "json"});        }

Login

 

以上就是关于json result的实例代码的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1432427.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
使用 VSCode 编写 .NET Core 项目的实例教程
上一篇 2025年12月17日 08:04:28
下一篇 2025年12月17日 08:04:59

相关推荐

发表回复

登录后才能评论
关注微信