【问题标题】:How to get Id from ASP.Net web api CreatedAtRoute with jquery如何使用 jquery 从 ASP.Net web api CreatedAtRoute 获取 Id
【发布时间】:2015-03-22 15:03:20
【问题描述】:

我正在尝试学习如何从在 asp.net web api 中创建帖子时创建的路由中获取 id

[ResponseType(typeof(MyDTO))]
public IHttpActionResult PostmyObject(MyDTO myObject)
{
...
return CreatedAtRoute("DefaultApi", new { id = myObject.Id }, myObject);
}

返回带有位置的 201,但我现在希望能够获取该位置的 id,而这里我不知道如何做到这一点

$.ajax({
statusCode: {
201: function() {
  //what to do here?
 }
}
});

【问题讨论】:

    标签: c# jquery ajax asp.net-web-api


    【解决方案1】:

    您应该使用以下内容检索 location 标头的值:

    $.ajax({
      statusCode: {
        201: function(data, textStatus, xhr) {
          //what to do here?
          console.log(xhr.getResponseHeader('Location')); 
        }
      }
    });
    

    【讨论】:

    • 非常感谢大卫,明天试试这个!
    猜你喜欢
    • 2016-10-16
    • 2017-10-06
    • 2015-12-24
    • 1970-01-01
    • 2023-01-11
    • 2017-08-16
    • 2020-10-04
    • 1970-01-01
    • 2015-05-14
    相关资源
    最近更新 更多