删除动词可在邮递员中使用,但不能与Ajax一起使用

编程入门 行业动态 更新时间:2024-10-24 00:21:21
本文介绍了删除动词可在邮递员中使用,但不能与Ajax一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我花了几个小时研究这个问题,只是无法弄清楚.

I have spent several hours researching this and just cannot figure it out.

首先我告诉你我拥有的东西

first I' show you what i have

js:

function deleteContact(id) { let url = baseUrl + "Contact/" + id; var data = { id : id } $.support.cors = true; $.ajax({ url: url, type: 'DELETE', dataType: 'json', data:data }); }

我有一个联系人控制器:

I have a Contact Controller:

public class ContactController : SimpleController<Contact> { public ContactController (IRepository<Contact> repository) : base(repository) { } }

和SimpleController:

and the SimpleController:

public abstract class SimpleController<T> : BaseController where T : class, IEntity, new() { private readonly IRepository<T> _repository; protected SimpleController (IRepository<T> repository) { _repository = repository; } [HttpGet] public virtual IEnumerable<T> Get () { var list = _repository.FindAll().ToList(); return list; } [HttpGet] public virtual T Get (Guid id) { var entity = _repository.Find(id); if ( entity == null ) throw new HttpResponseException(HttpStatusCode.NotFound); return entity; } [HttpPost] public virtual HttpResponseMessage Post (T entity) { if ( entity.Id != Guid.Empty ) throw new HttpResponseException(HttpStatusCode.BadRequest); _repository.Add(entity); _repository.Save(); var response = Request.CreateResponse<T>(HttpStatusCode.Created, entity); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = entity.Id })); return response; } [HttpPatch] public virtual T Patch (T entity) { var matchingItem = _repository.Find(entity.Id); if ( matchingItem == null ) throw new HttpResponseException(HttpStatusCode.NotFound); _repository.Update(entity); _repository.Save(); return entity; } [HttpDelete] public virtual void Delete (Guid id) { var matchingItem = _repository.Find(id); if ( matchingItem == null ) throw new HttpResponseException(HttpStatusCode.NotFound); _repository.Delete(id); _repository.Save(); } }

我会说我不是邮递员,这可以删除项目,但不会使用ajax.

I will say that i'n postman this works it deletes the item but it doesnt using the ajax.

我尝试了很多事情,但仍然保持405看起来像这样的回复

I have tried many things but still keep getting a 405 with a response that looks like this

{ "name": "Microsoft.ApplicationInsights.Dev.Request", "time": "2016-11-13T12:41:02.0356067Z", "tags": { "ai.device.roleInstance": "James-Desktop.JAILAFILES", "ai.operation.name": "OPTIONS Contact [id]", "ai.operation.id": "9UbG1l7oEJA=", "ai.internal.sdkVersion": "web: 2.1.0.363" }, "data": { "baseType": "RequestData", "baseData": { "ver": 2, "id": "9UbG1l7oEJA=", "name": "OPTIONS Contact [id]", "startTime": "2016-11-13T07:41:02.0356067-05:00", "duration": "00:00:02.4510180", "success": false, "responseCode": "405", "url": "localhost:52136/api/Contact/96d53daa-deca-4dbd-8c6d-2a236387d258", "httpMethod": "OPTIONS", "properties": { "DeveloperMode": "true" } } } }

我还应该补充一点,它永远不会到达称为Delete的方法,但确实会碰到简单的控制器构造函数.

I should also add that it never reaches the method called Delete but it does hit the simple controller constructor.

更新:

public Repository() { _context = new AlltechContext(); } public virtual T Add(T entity) { if (entity.Id == Guid.Empty) entity.Id = Guid.NewGuid(); return _context.Set<T>().Add(entity); } public virtual void Delete(Guid id) { var entity = _context.Set<T>().FirstOrDefault(x => x.Id == id); _context.Set<T>().Remove(entity); } public virtual T Find(Guid id) { return _context.Set<T>().FirstOrDefault(x => x.Id == id); } public int Save() { try { return _context.SaveChanges(); } catch (DbEntityValidationException ex) { var message = ex.EntityValidationErrors.Select(x=>x.ValidationErrors); throw new ArgumentException(message.ToString()); } } public virtual IEnumerable<T> FindAll () { return _context.Set<T>(); } public virtual T Update (T entity) { var actualEntity = _context.Set<T>().FirstOrDefault(x => x.Id == entity.Id); DuplicateItem(actualEntity, entity); _context.Set<T>().Attach(actualEntity); _context.Entry(actualEntity).State = EntityState.Modified; return entity; }

推荐答案

在App_Start下的Webconfig.cs中检查这些行是否存在

check if these lines are there in your Webconfig.cs under App_Start

using System.Web.Http; namespace WebService { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // New code config.EnableCors();//////////////////////////// config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } }

如果该行不可用,请转到软件包管理器控制台使用此命令

If that line is not available goto package manager console use this command

Install-Package Microsoft.AspNet.WebApi.Cors

然后您可能会对此行进行注释,如果需要取消注释的话.

And then you might have that line might be commmented, if so un comment it.

将此行添加到您的控制器类

add this line to your controller class

[EnableCors(origins: "localhost:55249",headers: "*", methods: "*")]

不要忘记包含名称空间

using System.Web.Http.Cors;

更多推荐

删除动词可在邮递员中使用,但不能与Ajax一起使用

本文发布于:2023-10-29 04:00:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538662.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:邮递员   可在   动词   但不   能与

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!