这可能是由于服务器上的防火墙或安全设置阻止了此API调用。为解决此问题,请在您的服务器上进行以下更改:
将防火墙设置以允许API调用。
确保API的URL在服务器上是正确的。
检查是否在控制器方法中启用了CORS功能。您可以使用以下代码在“Global.asax.cs”文件中启用CORS:
protected void Application_BeginRequest() { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); if (HttpContext.Current.Request.HttpMethod == "OPTIONS") { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); HttpContext.Current.Response.End(); } }
public JsonResult GetJsonData() { var data = new { name = "John", age = 30, city = "New York" }; return Json(data, JsonRequestBehavior.AllowGet); }
请确保在您的控制器方法中使用了正确的请求类型(GET或POST)和正确的JSON格式,并在API调用的URL中正确传递数据。
完成这些更改后,您应该能够在服务器上调用ASP.NET MVC控制器方法,并从中获取JSON数据。