jQuery Marvin.JsonPatch 无法识别 PATCH 请求
jQuery PATCH request not recognised by Marvin.JsonPatch
我正在尝试向 Web API 方法发送 AJAX PATCH 请求,并让 Marvin.JsonPatch 识别修补后的对象。
到目前为止,我发送到服务器的所有内容都导致收到空请求。
Web API 控制器方法如下所示:
public IHttpActionResult Update(ElementType elementType,
long elementId,
[FromBody] JsonPatchDocument<TranslationMatchDiscounts> matchDiscountsPatch)
而且 TranslationMatchDiscounts
对象非常简单:
public class TranslationMatchDiscounts
{
public double ContextMatches { get; set; }
public long ElementId { get; set; }
public Enumerations.ElementType ElementType { get; set; }
public double ExactMatches { get; set; }
public double PerfectMatches { get; set; }
public double Repetitions { get; set; }
}
而我的jQueryAJAX请求是这样组合的:
$.ajax({
//contentType: "application/json",
data: { "Repetitions": 0.15 },
//data: JSON.stringify({ "Repetitions": 0.15 }),
dataType: "json",
//processData: false,
method: "PATCH",
url: // my URL
});
注释掉的属性显示了我尝试过的一些东西。
控制器确实被击中,这不是问题,正在发送 "Repetitions" 属性 但服务器端 JsonPatchDocument<TranslationMatchDiscounts>
参数是...
null
如果我将 contentType
属性 留在原地
- 所有其他尝试都没有 properties/updates
使用 jQuery 的 .ajax() 发送 PATCH 请求的正确方法是什么?
我的错。我的印象是 PATCH 允许我们 post 一个仅具有更改属性的对象,即 ...
{
"Repetitions": 0.15
}
没有。 PATCH 需要的是 JSON patch document。根据 RTF:
A JSON Patch document is a JSON [RFC4627] document that represents an
array of objects.
所以我的例子真的应该是
var patchDoc = [ { "op": "replace", "path": "/Repetitions", "value": 0.15 } ];
$.ajax({
contentType: "application/json",
data: JSON.stringify(patchDoc),
dataType: "json",
method: "PATCH",
url: // my URL
});
我正在尝试向 Web API 方法发送 AJAX PATCH 请求,并让 Marvin.JsonPatch 识别修补后的对象。
到目前为止,我发送到服务器的所有内容都导致收到空请求。
Web API 控制器方法如下所示:
public IHttpActionResult Update(ElementType elementType,
long elementId,
[FromBody] JsonPatchDocument<TranslationMatchDiscounts> matchDiscountsPatch)
而且 TranslationMatchDiscounts
对象非常简单:
public class TranslationMatchDiscounts
{
public double ContextMatches { get; set; }
public long ElementId { get; set; }
public Enumerations.ElementType ElementType { get; set; }
public double ExactMatches { get; set; }
public double PerfectMatches { get; set; }
public double Repetitions { get; set; }
}
而我的jQueryAJAX请求是这样组合的:
$.ajax({
//contentType: "application/json",
data: { "Repetitions": 0.15 },
//data: JSON.stringify({ "Repetitions": 0.15 }),
dataType: "json",
//processData: false,
method: "PATCH",
url: // my URL
});
注释掉的属性显示了我尝试过的一些东西。
控制器确实被击中,这不是问题,正在发送 "Repetitions" 属性 但服务器端 JsonPatchDocument<TranslationMatchDiscounts>
参数是...
null
如果我将contentType
属性 留在原地- 所有其他尝试都没有 properties/updates
使用 jQuery 的 .ajax() 发送 PATCH 请求的正确方法是什么?
我的错。我的印象是 PATCH 允许我们 post 一个仅具有更改属性的对象,即 ...
{
"Repetitions": 0.15
}
没有。 PATCH 需要的是 JSON patch document。根据 RTF:
A JSON Patch document is a JSON [RFC4627] document that represents an array of objects.
所以我的例子真的应该是
var patchDoc = [ { "op": "replace", "path": "/Repetitions", "value": 0.15 } ];
$.ajax({
contentType: "application/json",
data: JSON.stringify(patchDoc),
dataType: "json",
method: "PATCH",
url: // my URL
});