也许这很愚蠢,但我想知道是否可以在 .Net MVC 的 Controller 中运行任何脚本,特别是 Ajax。
即我可以将它包装在任何东西中以使其在调用此方法时编译和工作吗?
[HttpPost]
public ActionResult apiLookUp()
{
$.ajax({
url: 'example.com/api',
type: 'GET',
dataType: 'json',
data: {
},
success: function (json) {
},
error: function (errorThrown) {
}
});
return Json(new { Success = json });
}
请您参考如下方法:
如果您尝试访问自己的资源之一,则无需进行 AJAX 调用。您已经在服务器上,可以实例化对象并直接进行调用。
但是,如果您的目标是调用外部站点,那么可以。
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://someServer.com/example.com/api");
myReq.ContentType = "application/json; charset=utf-8";
var response = (HttpWebResponse) myReq.GetResponse();
string text;
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
