2014-04-29 32 views
0

我有一個服務器方法,需要並編寫一個字符串列表,因爲IE9不讀取json格式,我想要的是設置字符串在http響應消息的內容。我似乎做得很好,但是當我得到響應時,我可以在身體上找到響應,我能看到的僅僅是響應消息中設置的長度。設置響應消息的內容,但我無法檢索它

這裏是代碼:

public HttpResponseMessage Upload() 
    { 
     HttpResponseMessage m = new HttpResponseMessage(HttpStatusCode.OK); 
     List<string> returnlist = new List<string>(); 
     for (int i = 0; i < Request.Files.Count; i++) 
     { 
        //string treatment   
     } 

     HttpResponseMessage response = new HttpResponseMessage(); 

     response.Content = new StringContent(string.Join(",", returnlist)); 
     return response; 
    } 

這就是字符串中包含「文件1,文件2,文件3」 ,但我無法找到它的Visual Studio或螢火

的答覆中說調試我得到的是:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers: 

{ 
    Content-Type: text/plain; charset=utf-8 
} 

我該如何設置並獲取響應消息?

在的情況下,需要的JS在這裏是:

self.uploadfiles = function() { 
     //startSpinner(); 

     if (!self.isOldIE()) { 
      var loc = window.location; 
      base_url = loc.protocol + "//" + loc.host; 
      baseUrl = base_url + "/"; 

      var save = true; 

      var xhr = new XMLHttpRequest(); 
      xhr.open('POST', baseUrl + 'Home/Upload'); 
      xhr.send(formdata); 
      xhr.onreadystatechange = function (data) { 
       //The responseText is the same as above 
       if (xhr.responseText.length > 0) { 

        //do stuff 
     } 
    }; 
+1

的狀態說,這一切...錯誤404:您請求的頁面不存在...您在上面顯示的代碼可能永遠不會被打... –

+0

這是因爲我正在處理js ajax中的響應,像一個json,我編輯了這個問題。 –

回答

0

試試這個使用獲得響應的Javascript:

var xhr = new XMLHttpRequest(); 

xhr.open('POST', baseUrl + 'Home/Upload', true); 
xhr.onreadystatechange = function() { 

    if (xhr.readyState === 4 && xhr.status === 200) { 
     alert(xhr.responseText); 
    } else { 
     // do something about the error 
    } 
}; 
xhr.send(formdata); 

注意的open()方法的第三個布爾參數獲取異步。

+0

無法解析符號Request.CreateResponse –

+0

我認爲你在ApiController中工作,但聽起來可能不是這種情況。保留你的原始代碼並嘗試新的答案來獲得答覆。 – McCee

+0

我正在使用Controller類,但我需要的是創建一個響應(不在json中使用ie9),並從客戶端使用js檢索它。客戶端已解決,但我沒有在響應中看到我發送的http消息中的字符串。 –

0

試試這個代碼

 var response = Request.CreateResponse(HttpStatusCode.OK, string.Join(",", returnlist)); 
     return response; 
0

出於某種原因,瀏覽器不會顯示HTTP響應的整個身體。所以我的解決方案是編寫響應頭並從js獲取頭文件。 也許有人知道瀏覽器爲什麼不顯示主體,或者有更好的解決方案。

這是我的控制器:

public HttpResponseMessage Upload() 
    { 
     HttpResponseMessage m = new HttpResponseMessage(HttpStatusCode.OK); 
     List<string> returnlist = new List<string>(); 


     for (int i = 0; i < Request.Files.Count; i++) 
     { 
      HttpPostedFileBase file = Request.Files[i]; //Uploaded file 
      //Use the following properties to get file's name, size and MIMEType 
      int fileSize = file.ContentLength; 

      if (file.FileName != "") 
      { 
       string fileName = string.Format("{0}.{1}", Guid.NewGuid().ToString(), file.FileName.ToString().Split('.')[1].ToString()); 
       returnlist.Add(fileName); 

      } 

     } 


//Here I write the response message headers 
     HttpResponseMessage response = new HttpResponseMessage(); 

     response.Content = new StringContent(string.Join(",", returnlist), Encoding.UTF8, "text/html"); 

     response.Headers.Add("data", string.Join(",", returnlist)); 

     return response; 
    } 

這裏是我如何得到頭回應:

var xhr = new XMLHttpRequest(); 
      xhr.open('POST', baseUrl + 'Home/Upload'); 
      xhr.send(formdata); 
      xhr.onreadystatechange = function (data) { 
       if (xhr.responseText.length > 0) { 
//header response 
        var response = xhr.responseText; 
       } 
相關問題