2010-10-21 150 views
0

這個jQuery AJAX調用C#web方法這裏是我的JS:爲什麼不工作

function declassifyAjax(e) { 

    var items = getSelected(); 
    var docIds = new Array(); 
    items.each(get); 

    //get ids of QcItem/docId we are dealing with 
    function get(count, el) { 
     docIds[count] = $(el).parent().attr('id'); 
    } 

    var dataObj = new Object(); 
    dataObj.batchId = batchId; 
    dataObj.docIds = docIds; 
    var dataString = JSON.stringify(dataObj) 


    //make call to webservice to get html to recreate view showing 
    //pending declassification 
    $.ajax({ 
     type: "POST", 
     url: applicationRoot + 'Models/BatchQC.asmx/declassify', 
     data: dataString, 
     contentType: "application/json; charset=utf-8", 
     success: function (data) { 
      if (ProcessWebMethodResult.processWebMethodResult(data) == true) { 
       declassifyProcess(data, e); 
      } 
     }, 
     error: function (e) { 
      alert("Failed to Get declassification details"); 
     } 
    }); 
} 

這裏是我的Web服務:

//type to represent the input the declassify method 
    public class DeclassifyType 
    { 
     public int batchId; 
     public string[] docIds; 
    } 

    [WebMethod(EnableSession = true)] 
    public WebMethodResult declassify(DeclassifyType dataString) 
    { 
    } 

任何和所有幫助表示讚賞!

Firebug中的調試顯示變量dataObj,batchId,docIds和dataString是正確的。我認爲我的Web方法簽名的設置方式有問題,因爲Ajax從未被解僱。逐句通過.ajax方法時,它會發生錯誤,而不是成功。

+1

當你說「不工作」時,哪部分過程不起作用?你期望發生什麼?什麼錯誤信息,如果有的話,你會得到什麼?您是否嘗試過使用[Firebug](http://getfirebug.com/)或類似軟件進行調試?請編輯您的問題並提供更多詳細信息。 – 2010-10-21 09:57:48

+0

'declassifyAjax'有沒有解僱?如果你在裏面放置一個alert()會發生什麼? – 2010-10-21 10:06:06

回答

2

您的Web方法需要一個參數,即您已擁有的數據對象,但是由於您直接傳遞對象而傳遞了多個參數。

相反,你需要有一個對象有一個屬性,dataString,並屬性的值應該是你的對象,像這樣:

var dataString = JSON.stringify({ dataString: dataObj }); 
            ▲--should match--▼ 
public WebMethodResult declassify(DeclassifyType dataString) 
1

啊,我只是固定它,

只是將簽名更改爲

[WebMethod(EnableSession = true)] 
public WebMethodResult declassify(int batchId, string[] docIds) 
{ 
} 

真的很簡單。感謝您檢查我的帖子!