{
HttpContext.Response.AppendHeader(Access-Control-Allow-Origin, *);
return Json(db.Weathers.ToList());
}
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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。