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); } })
nbsp;html><title>Index2</title><script></script><script>var login = function () { $.ajax({ type: "post", url: "http://localhost:4968/Weathers/JsonData", data: null, success: function (res) { alert(JSON.stringify(res)); }, dataType: "json"}); }</script><div> <a>ajax+Handler</a> <a>ajax+action</a> </div><div> <h3>Login</h3> <button>Submit</button> </div>
以上就是关于json result的实例代码的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号