Tag: asp.net web api

ASP.NET Web API – Ajax PUT – 405不允许的方法(entity framework)

我试图通过JQuery的Ajax函数向使用JSON到ASP.NET Web API的用户进行更新。 我一直从服务器获得405 (Method not Allowed)响应。 我已经尝试过几乎所有的东西,包括在web.config中禁用WebDAV模块,以及向我的web.config添加Access-Control-Allow-Origin标头。 我使用ADO模型和DbContext生成的数据库优先方法。 控制器也是由ASP.NET在创建时生成的。 我认为他们应该工作。 我试过PUT和POST都无济于事。 我不知道我做错了什么。 我的JS代码: function UpdateUserLoginTime(user) { $.getScript(“src/js/datetimeutils.js”); var url = “http://foo.bar.com:8081/api/user/”.concat(user.ID); var current = CurrentDateTime(); $.ajax({ url: url, type: ‘PUT’, data: JSON.stringify({“LastLoginDate” : current}), datatype: “json”, contentType: “application/json; charset=utf-8”, crossDomain: true, success: function (data) { alert(‘User login time updated sucessfully’); }, error: function () […]

从jqgrid调用asp.net web api DELETE的问题

我试图从jqGrid调用ASP.net WEB API的DELETE方法。 但是我收到消息 – “错误状态:’方法不允许’。错误代码:405” 。 以下是详细信息。 JQGrid代码 – $(“#employeeSalarysGrid”).jqGrid(‘navGrid’, ‘#employeeSalarysPager’, { add: true, edit: true, del: true }, editOption, addOption, delOption); var delOption = { caption: “Delete”, msg: “Delete selected record(s)?”, bSubmit: “Delete”, bCancel: “Cancel”, mtype: “DELETE”, url: ‘http://localhost:50570/api/Test/’ }; ASP.NET Web API方法: public HttpResponseMessage Delete(string id) { //code for processing delete request […]

JQuery AJAX’post’数据没有通过web api控制器

我正在将一个Person()对象设置为几个ko.observables并尝试将其传递给我的ajax调用。 另一方面,一个Web API控制器将其作为null。 这就是我对ko.observables的数据绑定方式: First Name Last Name Email Address * 我的.js假设要获取ko.observable值,但不是: var firstname = ko.observable(); var lastname = ko.observable(); var email = ko.observable(); var password = ko.observable(); function submitclicked() { insertNewUser(); ko.applyBindings(firstname); ko.applyBindings(lastname); ko.applyBindings(email); ko.applyBindings(password); }; function Person() { this.FirstName = firstname(); this.LastName = lastname(); this.Email = email(); this.Password = password(); } function insertNewUser() […]

MVC 4.5 Web API请求不起作用

我在.Net中设计了一个MVC 4.5 Web API。 HTTP请求必须来自Jquery AJAX Call。 在进行调用(POST)后,我在控制台中收到以下错误。 任何人都可以帮我确定我做错了什么吗? OPTIONS http://192.168.xx.xx:1245/api/values 405 (Method Not Allowed) jquery-1.7.1.min.js:4 OPTIONS http://192.168.xx.xx:1245/api/values Invalid HTTP status code 405 jquery-1.7.1.min.js:4 XMLHttpRequest cannot load http://192.168.xx.xx:1245/api/values. Invalid HTTP status code 405

如何从表单提交将产品和数量传递给ASP.NET MVC4 Web API

ASP.NET MVC4应用程序允许输入订购的产品数量并将它们传递给Web API控制器。 控制器接收空产品列表作为产品参数。 调试器显示数据已发布。 如何重构视图以便产品可以传递给Web API? 视图: @inherits ViewBase @using (Html.BeginForm()) { @for (int i = 0; i < Model.Products.Count; i++) { @Model.Products[i].Id @Html.HiddenFor(m => Model.Products[i].Id) @Html.TextBoxFor(m => Model.Products[i].Quantity, new { @class=”quantity”, type = “number”, min = 0 }) } @Html.HiddenFor(m => Model.CustomerId) } $(function () { “use strict”; var BASE_URL = ‘@Url.Content(“~/”)’; $(“form”).submit(function […]

403测试azure Blob存储上载时出错

我阅读并实现了以下文章,用于在javascript客户端和azure blob存储之间分块文件: http : //gauravmantri.com/2013/02/16/uploading-large-files-in-windows-azure-blob-storage-using- shared-access-signature-html-and-javascript 。 我似乎能够生成共享访问签名并创建权限,但是当我尝试使用以下SAS URL将块“突然”设置为azure时,我收到错误:“403(服务器无法validation请求确保正确形成Authorization标头的值,包括签名。)“。 有人可以取悦我做错了什么。 这是代码和url和代码: //sas url that is generated http://testing.blob.core.windows.net/image-container?sr=c&si=Perms1&sig=UowbDVCLfFdiVktTZuoupj6BiMUzLRxF3WEZlXKMJcA%3D&comp=block&blockid=YmxvY2stMDAwMDAw //Upload the blocks to azure storage if (evt.target.readyState == FileReader.DONE) { // DONE == 2 var uri = submitUri + ‘&comp=block&blockid=’ + blockIds[blockIds.length – 1]; var requestData = new Uint8Array(evt.target.result); $.ajax({ url: uri, type: “PUT”, data: requestData, processData: […]

在向Web API提交表单时如何获取响应状态

我有一个客户端HTML应用程序和一个Asp.net WebAPI作为服务器应用程序。 我有一个场景,我必须提交表单,作为表单提交的一部分,我需要将表单数据发布到数据库。 这对我有用,但客户端应用程序如何知道在不同域中发生的数据库操作的成功或失败状态。 我试图将HttpResponseMessage对象返回给客户端,但我的整个HTML页面都重写了我从服务器返回的状态。 有什么方法可以单独检查特定状态,而不是通过从服务器API应用程序获取响应来重写整个HTML,以便我在客户端应用程序中有更多的控制权? 提交表格的客户代码: function ValidateFileAndSubmit() { var myForm = $(“#form1”); //UploadFiles(); var rootServicePath = config.rootURL; var path = rootServicePath + ‘api/Upload/UploadFile’; myForm.attr(‘action’, path); myForm.submit(); } 我访问POST调用的Web Api代码: [HttpPost] public HttpResponseMessage UploadFile() { HttpResponseMessage response = null; if (HttpContext.Current.Request.Files.AllKeys.Any()) { HttpContext.Current.Response.ContentType = “text/HTML”; var httpPostedFile = HttpContext.Current.Request.Files[“UploadedImage”]; if (httpPostedFile != null) { […]

如何将没有超时的订购商品发布到控制器

HTML5离线ASP.NET MVC4应用程序允许输入订单数量并为客户提交订单。 在浏览器中呈现的产品列表包含约4000个产品。 只订购了少量特定订单的产品。 使用下面的代码请求超时。 浏览器发布所有产品。 请求花费太多时间,可能由httpruntime ExecutionTimeout终止 服务器错误日志显示已收到所有发布的数据。 看起来MVC模型绑定器需要太多时间来从发布的数据创建产品列表。 如何解决这个问题? 如何仅发布已输入数量或其他想法的订购商品? 如果这是合理的话,我可以切换到ajax,jquery和MVC4 Web API。 控制器: public class OfflineOrderController : ControllerBase { [HttpGet] public ActionResult Order(string customerId) { return View(new MobileOrderOrderViewModel(customerId)); } [HttpPost] public ActionResult Order(string customerId, [Bind(Prefix = “Products”)] IEnumerable Result) { … save order to database return new ContentResult() { Content = “Order […]

ASP.NET Web API 2 – POST / jQuery

我试图将一个对象发布到我使用Web API 2的服务器上。代码如下: $.ajax({ cache: false, type: “POST”, url: “/api/Employees”, data: { EmployeeId: 1 }, success: function(result) { console.log(“employees saved successfully”); }, error: function(result) { } }); 至于Web API: public class EmployeesController : ApiController { // POST api/ public void Post([FromBody]Employee value) { } } public class Employee { public Int32 EmployeeId { get; set; […]

CORS错误 – 使用NYTimes API,Ajax请求返回间歇性故障

我正在向NYTimes Congress API提出几个ajax请求,同时迭代一系列区域(1到5)。 我只能平均成功返回2个请求,其余的返回: XMLHttpRequest cannot load https://api.nytimes.com/svc/politics/v3/us/legislative/congress/members/house/OR/2/current.json?api-key=xxxxxxx. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:9001’ is therefore not allowed access. 似乎这些失败没有模式。 即使多次使用相同的_district数也看似随机失败。 我的代码: var title_element, title_render, district_numbers = [‘1’, ‘2’, ‘3’, ‘4’, ‘5’], element_ids = [‘ushd1’, ‘ushd2’, ‘ushd3’, ‘ushd4’, ‘ushd5’], apiKey = ‘api-key=xxxxxxxxxxx’, baseUrl = ‘https://api.nytimes.com/svc/politics/v3/us/legislative/congress/members/house/OR/’, orUsHouseMembers, frag; for (var i […]