Javascript 使用XMLHTMLRequest的OData批处理请求
我正在尝试编写一个javascript OData使用者,它将同时使用我的多个实体向我的WebApi服务器提交一篇文章(这样我就可以同时提交一个实体和关联的子实体)。但是,我遇到了一个恼人的错误:Javascript 使用XMLHTMLRequest的OData批处理请求,javascript,asp.net-web-api,odata,Javascript,Asp.net Web Api,Odata,我正在尝试编写一个javascript OData使用者,它将同时使用我的多个实体向我的WebApi服务器提交一篇文章(这样我就可以同时提交一个实体和关联的子实体)。但是,我遇到了一个恼人的错误: ExceptionMessage: "The message header 'POST /odata/MyEntity HTTP/1.1' is invalid. The header value must be of the format '<header name>: <head
ExceptionMessage: "The message header 'POST /odata/MyEntity HTTP/1.1' is invalid. The header value must be of the format '<header name>: <header value>'."
ExceptionType: "Microsoft.Data.OData.ODataException"
Message: "An error has occurred."
StackTrace: " at Microsoft.Data.OData.ODataBatchReaderStream.ValidateHeaderLine(String headerLine, String& headerName, String& headerValue)
↵ at Microsoft.Data.OData.ODataBatchReaderStream.ReadHeaders()
↵ at Microsoft.Data.OData.ODataBatchReaderStream.ProcessPartHeader()
↵ at Microsoft.Data.OData.ODataBatchReader.SkipToNextPartAndReadHeaders()
↵ at Microsoft.Data.OData.ODataBatchReader.ReadImplementation()
↵ at Microsoft.Data.OData.ODataBatchReader.ReadSynchronously()
↵ at Microsoft.Data.OData.ODataBatchReader.InterceptException[T](Func`1 action)
↵ at Microsoft.Data.OData.ODataBatchReader.Read()
↵ at System.Web.Http.OData.Batch.DefaultODataBatchHandler.<ParseBatchRequestsAsync>d__e.MoveNext()
↵--- End of stack trace from previous location where exception was thrown ---
↵ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
↵ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
↵ at System.Web.Http.OData.Batch.DefaultODataBatchHandler.<ProcessBatchAsync>d__0.MoveNext()
↵--- End of stack trace from previous location where exception was thrown ---
↵ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
↵ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
↵ at System.Web.Http.Batch.HttpBatchHandler.<SendAsync>d__0.MoveNext()"
我没有正确格式化请求吗?我一直在努力模仿这一点:
谢谢。我想你可以先用“添加引用”创建一个odata客户机。并使用Fiddler检查客户端发送的请求的有效负载。而且,您可以在AJAX中做同样的事情 我认为您可以先使用“添加引用”创建odata客户机。并使用Fiddler检查客户端发送的请求的有效负载。而且,您可以在AJAX中做同样的事情 我已经把这件事做好了。我通读了别人的代码()并创建了自己的实现(因为我不想使用JQuery)
我有点让它工作。我通读了别人的代码()并创建了自己的实现(因为我不想使用JQuery)
您有一个示例http请求吗?这可能是服务器问题,也可能是无效请求造成的。您有http请求示例吗?这可能是服务器问题,也可能是无效的请求造成的。嘿,这看起来很棒。它是支持ODataV4,还是专为v3设计的?谢谢@RobertMcLaws我为ODataV3设计了它,但后来没有iirc问题就转移到了ODataV4。嘿,这看起来很棒。它是支持ODataV4,还是专为v3设计的?谢谢@RobertMcLaws我为OData v3设计了它,但后来迁移到OData v4,没有iirc问题。
var oReq = new XMLHttpRequest();
oReq.onload = function () {
if (oReq.status == 404) {
defer.reject(oReq.statusText);
} else {
var response = JSON.parse(oReq.response);
if (response['odata.error']) {
defer.reject(oReq['odata.error']);
} else if (response.Message) {
defer.reject(response.Message);
} else {
defer.resolve(response);
}
}
};
oReq.onerror = function () {
defer.reject(oReq.statusText);
};
oReq.open("POST", "/odata/$batch", true);
var batch = "batch_" + newGuid();
oReq.setRequestHeader("Content-type", 'multipart/mixed; boundary="' + batch + '"');
var body = "--" + batch + "\r\n";
ko.utils.arrayForEach(entities, function (entity) {
body = body + [
'Content-Type: application/http; msgtype=request',
'',
'POST ' + url + ' HTTP/1.1',
'Content-Type: application/json; charset=utf-8',
'',
ko.toJSON(entity),
"--" + batch
].join('\r\n')
});
oReq.send(body + "--");
export interface ODataChangeRequest {
requestUri: string;
method: string;
data: string;
resolve: (response?: Object) => void;
reject: (response?: Object) => void;
}
export function packBatch(requests: ODataChangeRequest[], boundary: string): string {
var body = [];
body.push('--' + boundary);
var changeSet = 'changeset_' + newGuid();
body.push('Content-type: multipart/mixed; boundary=' + changeSet, '');
ko.utils.arrayForEach(requests, (d) => {
var t = d.method.toUpperCase();
body.push('--' + changeSet);
body.push('Content-Type: application/http', 'Content-Transfer-Encoding: binary', '');
body.push(t + ' ' + d.requestUri + ' ' + httpVersion);
body.push('Host: ' + location.host);
if (d.data) {
body.push('Content-Type: application/json; charset=utf-8');
body.push('', d.data, '');
}
});
body.push('--' + changeSet + '--', '');
body.push('--' + boundary + '--', '');
return body.join('\r\n');
}
export function unpackBatch(requests: ODataChangeRequest[], xhr: XMLHttpRequest): boolean {
var lines = xhr.responseText.split('\r\n'),
i = 0,
data = [],
feedingData = false,
statusCode = null,
allSuccessful = true;
ko.utils.arrayForEach(lines, (l) => {
if (!l.length) {
return;
}
var currentRequest = requests[i];
if (!currentRequest) {
return;
}
var httpIndex = l.indexOf(httpVersion);
if (httpIndex != -1) {
// Hadn't gotten data for previous request. Close
if (statusCode) {
if (statusCode == '200') {
currentRequest.resolve();
} else {
currentRequest.reject();
}
}
var startIndex = httpIndex + httpVersion.length + 1;
statusCode = l.substring(startIndex, startIndex + 3);
if (statusCode != '200') {
allSuccessful = false;
}
}
if (statusCode) {
if (l == "{") {
feedingData = true;
}
if (feedingData) {
data.push(l);
if (l == "}") {
var dataObj = data.length ? JSON.parse(data.join(" ")) : null;
if (statusCode == '200') {
currentRequest.resolve(dataObj);
} else {
currentRequest.reject(dataObj);
}
i++;
data = [];
feedingData = false;
statusCode = null;
}
}
}
});
return allSuccessful;
}