使用jQuery和listdata.svc更​​新SharePoint列表时无法获得Etag

时间:2012-06-07 19:10:32

标签: jquery rest sharepoint-2010

所以我使用jQuery与SharePoint的Rest服务(即listdata.svc)进行交互。当我调用listdata.svc来更新现有项目时 更新工作正常,但我无法得到listdata.svc为该项目发回的新ETag。在响应中发回的HTTP状态为204(无内容)。新的ETag在响应中以“ETag”标题发送(我可以在Fiddler中清楚地看到它)。但是在我的jQuery Ajax调用的“success:”或“complete:”回调中,如果我调用jqXHR.getResponseHeader(“ETag”),它总是返回null。它为Ii要求的任何头返回null。如果我调用jqXHR.getAllResponseHeaders(),则返回一个空字符串。

添加记录时遇到同样的问题。添加记录后,listdata.svc发送回ETag,以及请求标题中的新Item的URL,这是我无法获得的!

我正在使用jQuery-1.7.2,Internet Explorer版本8.0.7601.17514和SharePoint 2010 SP1。客户端操作系统是Windows 7。

我在一个简单的脚本中重新创建了该问题,该脚本试图在名为resttest的新TeamSite的任务列表中更新任务的标题。代码如下所示。

是否有人知道此问题的修复或解决方法?我测试了一下,看看我是否得到了与原生XMLHttpRequest相同的结果,而且也失败了。我得到1223的HTTP状态,client.getResponseHeader(“ETag”)返回一个空字符串。本机代码在下面进一步显示。

我在IE9中测试了tis,我得到了同样糟糕的结果。 我在Chome和Firefox中测试了int(使用XMLHttpRequest版本),client.getResponseHeader(“ETag”)重新调整了ETAG!在两个浏览器! (太糟糕了,我的yCOmpany标准版是IE!)。

这是使用jQuery的代码:

    _spBodyOnLoadFunctionNames.push("doit");
function doit(){
debugger;
$.ajax({
      url: "http://myServerName/resttest/_vti_bin/listData.svc/Tasks(1)",
      contentType: 'application/json; charset=utf-8',
      datatype: 'json',
      type: 'POST',
      async: true,    
      global:false,
      processData:false,
      data:"{Title:'Updated title'}",
      beforeSend: function (jqXHR, settings) {
                    jqXHR.setRequestHeader("X-HTTP-Method", "PUT");
                    jqXHR.setRequestHeader("If-Match", 'W/"2"');  // this must be set to the "Current" value of the etag on the server (I can get to it by calling http://myServerName/resttest/_vti_bin/listData.svc/Tasks(1)) from a browser
           },
      success: function (data, textStatus, jqXHR) {
            },
      complete: function (jqXHR, textStatus) {
                debugger;
                var x = jqXHR.getResponseHeader("ETag"); // this returns null , even though i see ETag: W/"2" in the headers in fiddler!
                var y = this.xhr();
                var z = y.getAllResponseHeaders();
                var kjh = y.getResponseHeader("ETag");
            },
      error: function (jqXHR, textStatus, errorThrown) {
                debugger;
                alert(jqXHR.responseText);
                alert('Something bad happened. Stopping');
            }
    });
}

以下是来自Fiddler的resposne标题:

HTTP/1.1 204 No Content
Cache-Control: no-cache
ETag: W/"2"
Server: Microsoft-IIS/7.5
SPRequestGuid: d0fc56f0-8277-469a-a290-e7d3de96295d
Set-Cookie: WSS_KeepSessionAuthenticated={b6a87457-6e7d-4de0-9c8d-f56add57f6f4}; path=/
X-SharePointHealthScore: 0
DataServiceVersion: 1.0;
X-AspNet-Version: 2.0.50727
Set-Cookie: WSS_KeepSessionAuthenticated={b6a87457-6e7d-4de0-9c8d-f56add57f6f4}; path=/
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 14.0.0.6106
Date: Thu, 07 Jun 2012 18:25:14 GMT

以下是使用XMLHttpRequest的代码:

_spBodyOnLoadFunctionNames.push("doit");
function doit() {
    debugger;
    var url = "http://sgm1dsps06/resttest/_vti_bin/listData.svc/Tasks(1)";
    var client = new XMLHttpRequest();
    client.open("POST", url, false);
    client.setRequestHeader("X-HTTP-Method", "PUT");
    client.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    client.setRequestHeader("If-Match", 'W/"5"');  // this must be set to the "Current" value of the etag on the server (I can get to it by calling http://sgm1dsps06/resttest/_vti_bin/listData.svc/Tasks(1)) from a browser
    client.send("{Title:'Updated title'}");
   {
       alert("Status is : " + client.status + " " + client.statusText + ".");  //the status is always 1223
       var etag = client.getResponseHeader("ETag");
       if (etag == null)
           alert("etag is null"); 
       else
           alert("Etag is " + etag); // the etag is alwasys an empty string

    }
}

1 个答案:

答案 0 :(得分:1)

我找到了解决这个问题的可行方案。由于我们无法获取原始PUT请求返回的http标头,因此我只是从原始“PUT”请求的成功回调中向该资源发出了一个新查询

>

 _spBodyOnLoadFunctionNames.push("doit");
function doit(){
debugger;
$.ajax({
      url: "http://sgm1dsps06/resttest/_vti_bin/listData.svc/Tasks(1)",
      contentType: 'application/json; charset=utf-8',
      datatype: 'json',
      type: 'POST',
      async: true,    
      global:false,
      processData:false,
      data:"{Title:'Updated title'}",
      beforeSend: function (jqXHR, settings) {
                    jqXHR.setRequestHeader("X-HTTP-Method", "PUT");
                    jqXHR.setRequestHeader("If-Match", 'W/"3"');  // this must be set to the "Current" value of the etag on the server (I can get to it by calling http://sgm1dsps06/resttest/_vti_bin/listData.svc/Tasks(1)) from a browser
           },
      success: function (data, textStatus, jqXHR) {
            },
      complete: function (jqXHR, textStatus) {
               debugger;
                if (jqXHR.status === 204) {
                    // requery the resource to get the new etag // this is due to a bug in ie that drops all headers associated with an http 204
                    $.ajax({
                        async: false,
                        contentType: 'application/json; charset=utf-8',
                        datatype: 'json',
                        url: this.url,
                        success: function (data, textStatus, jqXHR) {
                            var etag = jqXHR.getResponseHeader("ETag");
                            alert(etag);
                        }
                    }
                  }
            },
      error: function (jqXHR, textStatus, errorThrown) {
                debugger;
                alert(jqXHR.responseText);
                                }
    });
}