Tag: asp.net web api

传递凭证不起作用

我试图通过传递凭证来进行json调用。 如果用户已使用Sql Membership登录系统,如何在不获取此信息的情况下传递凭据: 这是调用web.api的代码: 注意:正在从跨域调用:即www.project.com – > api.project.com var url = ‘http://api.project.com/LookUps/uploadtypes?callback=?’; $.ajax({ type: ‘GET’, url: url, async: true, xhrFields: { withCredentials: true }, jsonpCallback: ‘handler’, contentType: “application/javascript”, dataType: ‘jsonp’, success: function (json, textStatus, jqxhr) { console.log(jqxhr.responseText); }, error: function (json, textStatus, jqxhr) { console.log(jqxhr.responseText); } }); 编辑: 禁用身份validation时,我仍然可以登录。 是否有另一个窗口validation设置的地方?

使用WEB API的Ajax Jquery请求

我正在尝试从WEB API调用中检索JSON结果。 我的WEP API方法: [AcceptVerbs(“GET”, “POST”)] public object GetTest() { rep = new ChatRepository(); chatBoxCLS box = rep.Chatrequest(chatRequestLevel.Parent, null); System.Web.Mvc.JsonResult jsonResult = new System.Web.Mvc.JsonResult { Data = box, JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet }; return jsonResult.Data; } 我已经修改了WebapiConfig.cs,如下所示,它将始终返回JSON config.Routes.MapHttpRoute( name: “DefaultApi”, routeTemplate: “api/{controller}/{action}/{id}”, defaults: new { action = “get”, id = RouteParameter.Optional } ); var appXmlType = […]

来自Web Form的Web-API cal – AJax。 没有’Access-Control-Allow-Origin’标题

我试图使用Web API与Web窗体应用程序通过Ajax调用获取数据。 因此创建了一个Web API项目并将其部署到本地IIS。 它回应了这些要求。 然后在webForm(Default.aspx)中进行Ajax调用以获取数据。 $(document).ready(function () { var url = ‘http://localhost:8080/api/Values’; $.ajax({ url: url, type: ‘POST’, crossdomain: ‘true’, contentType: “application/json; charset=utf-8;”, type: ‘GET’, success: function (result) { var r = JSON.stringify(result); alert(“From Web-API ” + r); } }); }); 但这是跟随错误 “XMLHttpRequest无法加载http:// localhost:8080 / api / Values 。请求的资源上没有’Access-Control-Allow-Origin’标头。因此不允许来源’ http:// localhost:61605 ‘。 “ 我在这里缺少哪个标题?

即使我可以看到JSON有效负载返回,jQuery ajax调用跨域返回状态404

我今天大部分时间都在试图找出为什么我的独立HTML页面中的jQuery ajax调用跨域ASP.Net MVC Web API调用返回正确的JSON数据,但是我收到以下错误消息在我的ajax调用的错误部分: 对象{readyState = 4,status = 404,statusText =“error”,更多…} 我正在使用jQuery 2.0.3。 这是我的ajax调用代码: $.ajax ({ type: “GET”, beforeSend: function (xhr, settings) { xhr.setRequestHeader(“Authorization”, “95d11869-a2ad-4925-8a16-ee23c82909ac”); }, url: url, dataType: “json”, processData: false, crossDomain: true, success: function (jsonFromServer) { alert(JSON.stringify(jsonFromServer)); }, error: function (xhr, textStatus, errorThrown) { if (typeof console === ‘object’ && typeof console.log === […]

AJAX将多个参数传递给WebApi

AJAX请求: $.ajax({ url: url, dataType: ‘json’, type: ‘Post’, data: {token:”4″, feed:{“id”:0,”message”:”Hello World”,”userId”:4} } }); 服务器端Web API: [HttpPost] public HttpResponseMessage Post(string token, Feed feed) { /* Some code */ return new HttpResponseMessage(HttpStatusCode.Created); } 错误代码404:{“message”:“找不到与请求URI匹配的HTTP资源’localhost:8080 / api / feed’。”,“messageDetail”:“在控制器’Feed’上找不到与之匹配的操作请求。”} 为什么我收到此错误以及为什么我无法将多个参数POST到我的API?

Jquery Ajax调用WEB API

我正在尝试对WEB API方法进行简单的jquery ajax调用。 $(document).ready(function () { $.ajax({ type: ‘POST’, url: ‘http://redrock.com:6606/api/values/get’, dataType: “jsonp”, crossDomain: true, success: function (msg) { alert(“success”); }, error: function (request, status, error) { alert(error); } }); }); WEB API动作: public IEnumerable Get() { return new string[] { “value1”, “value2” }; } ajax调用没有访问WEB API。 我在浏览器控制台中收到以下错误。 获取http://redrock.com:6606/api/values/get?callback=jQuery18207315279033500701_1383300951840&_=1383300951850 400(错误请求)

Cors,Web Api,IE8,Post Complex Data

作为我工作环境的一部分,我们需要支持IE8,但希望继续推进技术,特别是CORS。 我在将复杂对象发布到ie8中的cors服务时遇到了麻烦。 该对象为null。 以下是重现的步骤。 如果需要,我可以将项目上传到github。 我创建了一个新的mvc4项目。 添加了API控制器。 并做了以下更改。 支持预检复杂的cors调用(global.asax): protected void Application_BeginRequest() { //This is needed for the preflight message //https://stackoverflow.com/questions/13624386/handling-cors-preflight-requests-to-asp-net-mvc-actions if (Request.Headers.AllKeys.Contains(“Origin”) && Request.HttpMethod == “OPTIONS”) { Response.Flush(); } } 来源: 处理对ASP.NET MVC操作的CORS预检请求 支持text / plain(ie8只发送带有cors的text / plain)(global.asax): protected void Application_Start() { //This is needed to support text/plain HttpConfiguration config = GlobalConfiguration.Configuration; config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue(“text/plain”)); […]

Access-Control-Allow-Origin标头具有多个值或预检jquery ajax到Web Api 2

我有一个在localhost上运行的asp.net Web表单应用程序:6414然后jquery ajax在localhost上调用Web Api服务 :11974 我收到错误 The ‘Access-Control-Allow-Origin’ header contains multiple values ‘*, *’, but only one is allowed. Origin ‘http://localhost:6414’ is therefore not allowed access. 所以我梳理了google和stackoverflow ,发现了大量有冲突的 信息 。 1. Fix with web.config 2. Fix with jquery ajax call 3. Fix with CORS in web.api 我已经尝试了Cors启用和web.config以及各种技术 。 目前我想知道问题实际上是Web表单jquery代码作为发送者,但实际上响应头部意味着它来自web api服务… 我看看我的响应标题,我看到其中的两个 Access-Control-Allow-Origin:* ( repeated TWICE) […]

如何通过AngularJS $ http从ASP.Net Web API 2获取Access Token?

我试着这样: $http({ method: ‘POST’, url: ‘/token’, data: { username: $scope.username, password: $scope.password, grant_type: ‘password’ } }).success(function (data, status, headers, config) { $scope.output = data; }).error(function (data, status, headers, config) { $scope.output = data; }); 然后尝试将grant_type更改为param: $http({ method: ‘POST’, url: ‘/token’, data: { username: $scope.username, password: $scope.password }, params: { grant_type: ‘password’ } }).success(function (data, status, […]

Angularjs + ASP.NET MVC + ASP.NET Web API

我打算构建我的新Web应用程序。 我擅长ASP.NET MVC和jQuery,也有Web API的工作知识。 我是Angularjs的新手,但我想在客户端使用它来处理需要DOM操作的复杂页面。 我有以下问题: 在知道单页应用程序框架是一个具有许多页面和复杂逻辑/算法的中型应用程序之后,我是否还应该考虑它。 我听说人们谈论用ASP.NET Web API + Angularjs完全替换ASP.NET MVC + jQuery 。 对于具有复杂逻辑的中型或大型Web应用程序来说,它真的是一个更好的选择。 如果是,那么会话,认证等的替代方案是什么 将ASP.NET Web API,ASP.NET MVC和Angularjs一起使用是一个明智的决定。 最新版本的jQuery,Bootstrap和Angularjs之间的兼容性是什么? 使用Angularjs + SPA而不是ASP.NET MVC + jQuery(非SPA)会增加开发时间/成本 。 是否有任何机构使用Angularjs编写中/大型复杂Web应用程序?