2015-06-01 69 views
0

我需要從我的網頁調用WCF服務。服務中的方法使用泛型列表作爲參數,我需要知道如何構造ajax查詢?如何框架AJAX請求

方法:

public List<FileListEntity> ListLogFiles(string status, List<FileListEntity> fileList) 
{ 
    .............//implementation.... 
    return fileList; 
} 

當方法被稱爲「的fileList」作爲傳遞null一日一次,這種方法填充列表和發送回網站。第二次,網站將收到的fileList以及一些狀態發送回服務。

AJAX查詢:

var status = defineStatus(); 
var value; 
var jsonObj; 
function callLogService(){ 
    debugger; 

    if (value == null) { 
     jsonObj = "null"; 
    } 
    else { 
     jsonObj=value; 
    } 
    $.ajax({ 
     type: "POST", 
     url: "http://localhost:56256/SearchService.svc/ListLogFiles", 
     data: '{"status":"'+ status+'","fileList":'+jsonObj+' }', 
     crossDomain: true, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     processdata: true, 
     success: function (response) { 
      value = response; 
      display(value); 
      debugger; 
      alert("Success"); 
      //debugger; 
     }, 
     error: function() { 
      alert("An error has Occured, Please try refreshing !"); 
     } 
    }); 

} 

這工作第1次時jsonObj爲空,但不工作的時候jsonObj包含值的第二次。 它說:

無法加載資源:服務器500(內部服務器錯誤)的狀態

我認爲這說法是不是在第二時間傳遞正確迴應。 有什麼想法?如何解決。

回答

0

終於找到了答案,我發送給服務的數據是錯誤的。 第一變化

if (value == null) { 
    jsonObj = "null"; 
} 
else { 
    jsonObj=value.d; 
} 

value.d(或value.data在一些情況下,檢查使用調試器)包含實際的數據。

第二變化, 裏面的AJAX查詢寫:

data: JSON.stringify({ status: status, fileList: jsonObj }) 

,而不是data: '{"status":"'+ status+'","fileList":'+jsonObj+' }',