2012-06-07 66 views
0

因此,我使用jQuery與SharePoint Rest Services(即listdata.svc)進行交互。當我打電話給listdata.svc更​​新一個現有的項目 更新正常,但我無法得到新的ETag listdata.svc發送回該項目。在響應中發回的HTTP狀態是204(無內容)。新的ETag在響應中發送到'ETag'標題中(我可以在Fiddler中清楚地看到它)。但是,在我的jQuery Ajax調用的「success:」或「complete:」回調中,如果我調用jqXHR.getResponseHeader(「ETag」),它總是返回null。它爲任何標題Ii要求返回null。如果我調用jqXHR.getAllResponseHeaders(),它將返回一個空字符串。使用jQuery和listdata.svc更​​新SharePoint列表時無法獲取Etag

我在添加記錄時遇到同樣的問題。在添加記錄後,listdata.svc將ETag以及請求標題中的新項目的URL發送回去,這是我無法理解的!

我使用jQuery-1.7.2,Internet Explorer版本8.0.7601.17514和SharePoint 2010 SP1。客戶端操作系統是Windows 7.

我在一個簡單的腳本中重新創建了該問題,該腳本試圖更新稱爲resttest的新TeamSite的任務列表中的任務標題。代碼如下所示。

有沒有人知道這個問題的解決方法或解決方法?我測試瞭解我是否得到了使用本機XMLHttpRequest獲得的相同結果,但也失敗了。我得到一個1223的HTTP STATUS,client.getResponseHeader(「ETag」)回退一個空字符串。本機代碼顯示在下面。

我在IE9中測試了它,並且得到了同樣不好的結果。 我在Chome和Firefox(使用XMLHttpRequest版本)中測試了int,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'); 
      } 
    }); 
} 

這裏有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

我找到了一個可行的解決這個問題。既然我們不能用原來的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); 
           } 
    }); 
} 
相關問題