使用jQuery.getJson获得网页API [英] Use jQuery.getJson to get Web API
问题描述
我的ASP.NET Web API的初学者。结果
未能使用 jQuery.getJson()
获得的ASP.NET Web API
这失败:
块引用>//在文件:/// C:/Users/lil/Desktop/index.html
VAR URL =HTTP://本地主机:56110 / API /值;
$ .getJSON(URL,功能(数据){
$(#locMsg)文本(成功+数据)。
});`
这成功了:
块引用>//中的http://本地主机:56110 / index.html的
VAR URL =HTTP://本地主机:56110 / API /值;
$ .getJSON(URL,功能(数据){
$(#locMsg)文本(成功+数据)。
});
我虽然是因为跨域请求的,但这种成功
块引用>//在文件:/// C:/Users/lil/Desktop/index.html
VAR URL = \"http://api.flickr.com/services/feeds/photos_public.gne?tags=dog&tagmode=any&format=json&jsoncallback=?\";
$ .getJSON(URL,功能(数据){
$(#locMsg)文本(成功);
});
然后我尝试添加jsoncallback =?但也失败了:
块引用>//在文件:/// C:/Users/lil/Desktop/index.html
VAR URL =HTTP://本地主机:56110 / API /值= jsoncallback?
$ .getJSON(URL,功能(数据){
$(#locMsg)文本(成功+数据)。
});
ValuesController:
块引用>命名空间WebApplication1.Controllers {
公共类ValuesController:ApiController
{
//获取API /值
公共IEnumerable的<串GT;得到()
{
返回新的字符串[] {值1,值2};
}` `//获取API /价值/ 5
公共字符串GET(INT ID)
{
回到价值;
} // POST API /值
公共无效后([FromBody]字符串值)
{
} // PUT API /价值/ 5
公共无效认沽(INT ID,[FromBody]字符串值)
{
} //删除API /价值/ 5
公共无效删除(INT ID)
{
}
}}
}解决方案您必须启用CORS在你的WebAPI。首先,安装这个的NuGet - https://www.nuget.org/packages/Microsoft .AspNet.WebApi.Cors 然后将此行添加到WebApiConfig:
config.EnableCors(新EnableCorsAttribute(*,*,*));
WebApiConfig:
公共静态类WebApiConfig
{
公共静态无效的注册(HttpConfiguration配置)
{ config.EnableCors(新EnableCorsAttribute(*,*,*)); //网页API的配置和服务
//配置Web API只使用承载令牌认证。
CONFIG.SUP pressDefaultHostAuthentication();
config.Filters.Add(新HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); //网页API路线
config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(
名称:DefaultApi
routeTemplate:API / {}控制器/(编号),
默认:新{ID = RouteParameter.Optional}
);
}I'm a beginner of ASP.NET Web API.
Fail to usejQuery.getJson()
to getASP.NET Web API
this failed:
//in "file:///C:/Users/lil/Desktop/index.html" var url = "http://localhost:56110/api/Values"; $.getJSON(url, function (data) { $("#locMsg").text("success"+data); });`
this succeeded:
//in "http://localhost:56110/index.html" var url = "http://localhost:56110/api/Values"; $.getJSON(url, function (data) { $("#locMsg").text("success"+data); });
I though it is because of cross-domain request, but this succeeded:
//in "file:///C:/Users/lil/Desktop/index.html" var url = "http://api.flickr.com/services/feeds/photos_public.gne?tags=dog&tagmode=any&format=json&jsoncallback=?"; $.getJSON(url, function (data) { $("#locMsg").text("success"); });
then I tried to add "jsoncallback=?" but also failed:
//in "file:///C:/Users/lil/Desktop/index.html" var url = "http://localhost:56110/api/Values?jsoncallback=?"; $.getJSON(url, function (data) { $("#locMsg").text("success"+data); });
ValuesController:
namespace WebApplication1.Controllers{ public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; }` `// GET api/values/5 public string Get(int id) { return "value"; } // POST api/values public void Post([FromBody]string value) { } // PUT api/values/5 public void Put(int id, [FromBody]string value) { } // DELETE api/values/5 public void Delete(int id) { } }
} }
解决方案You need enable CORS in you WebAPI. Firstly, install this Nuget - https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Cors and then add this line to WebApiConfig:
config.EnableCors(new EnableCorsAttribute("*","*","*"));
WebApiConfig:
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.EnableCors(new EnableCorsAttribute("*","*","*")); // Web API configuration and services // Configure Web API to use only bearer token authentication. config.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }
这篇关于使用jQuery.getJson获得网页API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!