2014-11-24 49 views
1

我目前正在研究一些可以包含在使用TrueSample的調查報頭中的JavaScript,並將動態生成並激發調查的Webservice調用。 Truesample的一個要求是,在每個頁面之後,它會發送該頁面上花費的時間以及在調查開始時生成的一些其他任意信息。我試圖自動化每個頁面的Web服務調用,這樣我就不必在每次調查中都有數百個Web服務。如何使用ajax請求以JavaScript方式發起Webservice調用

我很遠,並已發現一些很酷的技巧,使這一切工作,但我掙扎着使用JavaScript解僱web服務。

這是我到目前爲止有:

Qualtrics.SurveyEngine.addOnload(function() 
{ 
       var pageStart = new Date(); 
       var beginning = pageStart.getTime(); 
       // Necessary Variables 
       var account-id = parseInt("${e://Field/account-id}"); 
       var passcode = parseInt("${e://Field/passcode}"); 
       var survey-country = parseInt("${e://Field/survey-country}"); 
       var end-client-id = parseInt("${e://Field/end-client-id}"); 
       var page-exposure-duration; 
       var page-id = parseInt("${e://Field/pageID}"); 
       var platform-id = parseInt("${e://Field/platform-id}"); 
       var respondent-id = parseInt("${e://Field/respondent-id}"); 
       var response-id = parseInt("${e://Field/response-id}"); 
       var source-id = parseInt("${e://Field/source-id}"); 
       var survey-id = parseInt("${e://Field/survey-id}"); 
       var api-version = parseInt("${e://Field/api-version}"); 
       //End Variables 
       var that = this; 
       that.hideNextButton(); 
       var para = document.createElement("footnote"); 
       var test = document.getElementById("Buttons"); 
       var node = document.createElement('input'); 
       var next = document.getElementById("NextButton"); 
       node.id = "tsButton"; 
       node.type = "button"; 
       node.name = "tsButton"; 
       node.value = " >> "; 
       node.onclick = function trueSample(){ 
           var pageEnd = new Date(); 
           var end = pageEnd.getTime(); 
           var time = end - beginning; 
           window.alert(pageID + ", time spent on page = " + time); 
     Qualtrics.SurveyEngine.setEmbeddedData("pageID", pageID + 1); 

           new Ajax.Request('webserviceURL', { 
           parameters: { 
               account-id: account-id, 
               passcode: passcode, 
               survey-country: surveycountry, 
               end-client-id: end-client-id, 
               page-exposure-duration: time, 
               page-id: page-id, 
               platform-id: platform-id, 
               respondent-id: respondent-id, 
               response-id: response-id, 
               source-id: source-id, 
               survey-id: survey-id, 
               api-version: api-version} 


           }); 

           that.clickNextButton(); 
       }; 
       para.appendChild(node); 
       test.insertBefore(para, next); 

}); 

有沒有人有射擊webservice的召喚出來的Javascript的經驗?如果是這樣,你有什麼想法如何完成ajax請求,並使其工作?還是有另一種(可能更好)的方法,我可以使用這些電話將工作?我明白在Stack Overflow上有這方面的信息,但我很難理解特定的用例如何適用於我的。

此外,請注意,儘管我很喜歡使用JQuery,但我僅限於vanilla Javascript和Prototype.JS。

回答

0

使用傳統的JavaScript XmlHttpRequest,您可以進行AJAX調用。對於Webservice,我們需要幾個HTTP頭。如:SOAPAction,Content-Type,Accept。這些頭的值必須是象下面這樣:

SOAPAction:"" 
Content-Type:text/xml 
Accept:text/xml 

所以,另外,你的代碼應該是這個樣子製作一個AJAX調用web服務:

//Get XML Request Object 
var request = new XMLHttpRequest(); 
// Define the URL 
var url="http://your.end.point.url?wsdl"; 
//Define HTTP Method. Always POST for a Webservice 
request.open("POST", url, true); // Remember that all the Webservice calls should be POST 
//Setting Request Headers 
request.setRequestHeader("SOAPAction", "\"\"");//Not sure of the escape sequence. The value should be "". 
request.setRequestHeader("Accept","text/xml"); 
request.setRequestHeader("Content-Type","text/xml"); 

//Make your AJAX call  
request.send(soap); // where soap is you SOAP Request Payload. 

解析響應:

request.onreadystatechange=stateChanged; 

function stateChanged() 
{ 
if (request.status==200) 
{ 
// Success. Parse the SOAP Response 
} 
if(request.status==500) 
{ 
//Failure. Handle the SOAP Fault 
} 
} 
+0

這是完美的,我試圖實現,謝謝一堆! – 2014-11-24 19:20:09

+0

很高興我有幫助! – Bhaskara 2014-11-24 19:29:49

+0

後續,它看起來像我遇到了使用此問題,因爲它是一個單獨的域Web服務,並且我無法訪問目標Web服務以允許源域。我在這裏閱讀第二個答案http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript似乎涵蓋我的情況,但我有一些麻煩了解它應該如何工作。任何信息,將不勝感激。 – 2014-12-03 17:18:23