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); } })
<!doctype html><html><head runat="server"><title>index2</title><script src="\scripts\jquery-1.10.2.min.js?1.1.11" type="text/javascript"></script><script type="text/javascript">var login = function () {
$.ajax({ type: post, url: http://localhost:4968/weathers/jsondata, data: null, success: function (res) {
alert(json.stringify(res));
}, datatype: json});
}</script></head><body><div id="nav"><a href="/home/index">ajax+handler</a> <a>ajax+action</a></div><div><h3>login</h3><button type="button" onclick="login()">submit</button></div></body></html>
以上就是关于json result的实例代码的详细内容。