2010-08-25 133 views
1

我正在開發SilverLight應用程序,其中在瀏覽器關閉事件中,我需要執行Web服務調用。我有一個接受一個參數的Web服務方法。當用戶點擊瀏覽器關閉事件時。我將會調用doRelease()函數。 releaseuser方法需要一個參數usertoken。JQuery:調用Web服務

我在調用我的jQuery函數CallService()時出錯。

Line: 186 Error: Object expected

var varType; 
    var varUrl; 
    var varData; 
    var varContentType; 
    var varDataType; 
    var varProcessData; 
    //Generic function to call AXMX/WCF Service   
    function CallService() { 
     $.ajax({ 
      type: varType, //GET or POST or PUT or DELETE verb 
      url: varUrl, // Location of the service 
      data: varData, //Data sent to server 
      contentType: varContentType, // content type sent to server 
      dataType: varDataType, //Expected data format from server 
      processdata: varProcessData, //True or False 
      success: function (msg) {//On Successfull service call 
       alert("success"); 
       ServiceSucceeded(msg); 
      }, 
      error: ServiceFailed// When Service call fails 
     }); 
    } 

    function Temp(usertoken) { 
     varType = "POST"; 
     varUrl = "http://localhost/TempWS/MachineHistoryWS.asmx?op=ReleaseUser"; 
     varData = usertoken; 
     varContentType = "application/json; charset=utf-8"; 
     varDataType = "json"; 
     varProcessData = true; 

     alert("call service"); 

     CallService(); 

    } 
    function ServiceSucceeded(result) {//When service call is sucessful 

     alert("success"); 

     varType = null; varUrl = null; varData = null; varContentType = null; varDataType = null; varProcessData = null; 
    } 
    function ServiceFailed(result) { 
     alert('Service call failed: ' + result.status + '' + result.statusText); 
     varType = null; varUrl = null; varData = null; varContentType = null; varDataType = null; varProcessData = null; 
    } 



    function doRelease() { 

     var usertoken = readCookie("usertoken"); 


     Temp("usertoken"); 
    } 
+1

由於在你的例子中少於50行,你可以給我們所有的提示什麼代碼是在你的實際頁面186行嗎? :) – 2010-08-25 11:11:22

+0

它在進入函數CallService()時發生。你可以說發送我的參數?這是對的嗎? – xscape 2010-08-25 11:25:55

+0

這與Silverlight有什麼關係? – Pointy 2010-08-25 12:38:25

回答

1

我解決了我的問題,但沒有使用jQuery。這是我的解決方案。

function sendDataAsXML_SOAP() { 
     var req_params = "", url = "", number = 0, type = ""; 
     /* Configure Parameters */ 
     url = "http://localhost/TempWS/MachineHistoryWS.asmx"; 
     user = "129272802615082804"; 

     req_params = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"; 
     req_params = req_params + "<soap:Body><ReleaseUser>"; 
     req_params = req_params + "<credentials>" + user + "</credentials></ReleaseUser></soap:Body></soap:Envelope>"; 
     alert(req_params); 
     /* Send XML/SOAP Request To Web Service Using Browser's Javascript DOM */ 
     try { 
      ajax_request = new XMLHttpRequest(); 
     } 
     catch (trymicrosoft) { 
      try { 
       ajax_request = new ActiveXObject("Msxml2.XMLHTTP"); 
      } 
      catch (othermicrosoft) { 
       try { 
        ajax_request = new ActiveXObject("Microsoft.XMLHTTP"); 
       } 
       catch (failed) { 
        ajax_request = false; 
       } 
      } 
     } 
     ajax_request.open("POST", url, true); 
     ajax_request.setRequestHeader("Content-Type", "text/xml;charset=utf-8"); 
     ajax_request.onreadystatechange = receiveXML_SOAPData; 
     ajax_request.send(req_params); 
    } 

    function receiveXML_SOAPData() { 
     if (ajax_request.readyState == 4) { 
      if (ajax_request.status == 200) { 
       alert(ajax_request.responseText); 

      } 
     } 
    } 
0

這看起來有點奇怪: -

function doRelease() { 

    var usertoken = readCookie("usertoken"); 


    Temp("usertoken"); 
} 

首先,我們假設readCookie是做正確的事?

其次應該是最後一行是: -

Temp(usertoken); 

三,凡在這一切的 「Silverlight的」 角?

0

數據必須包裝爲一個對象。在CallService功能,更改:

data: varData, 

到:

data: "{input:'" + varData + "'}", 

「INPUT」 改變實際參數名稱在Web服務方法。