我目前正在研究一些可以包含在使用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。
这是完美的,我试图实现,谢谢一堆! – 2014-11-24 19:20:09
很高兴我有帮助! – Bhaskara 2014-11-24 19:29:49
后续,它看起来像我遇到了使用此问题,因为它是一个单独的域Web服务,并且我无法访问目标Web服务以允许源域。我在这里阅读第二个答案http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript似乎涵盖我的情况,但我有一些麻烦了解它应该如何工作。任何信息,将不胜感激。 – 2014-12-03 17:18:23