Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ajax请求在Javascript中触发Webservice调用_Javascript_Ajax_Web Services_Prototypejs_Qualtrics - Fatal编程技术网

如何使用ajax请求在Javascript中触发Webservice调用

如何使用ajax请求在Javascript中触发Webservice调用,javascript,ajax,web-services,prototypejs,qualtrics,Javascript,Ajax,Web Services,Prototypejs,Qualtrics,我目前正在开发一些javascript,这些javascript可以包含在使用TrueSample的调查的标题中,并将为调查动态生成和激发Webservice调用。Truesample的一个要求是,在每个页面之后,都会发送在该页面上花费的时间,以及在调查开始时生成的一些其他任意信息。我正在尝试自动化每个页面的web服务调用,这样我就不必在每次调查中都有数百个web服务 我已经走了很长的路,并且已经找到了一些很酷的技巧来让这一切正常工作,但是我正在努力使用javascript启动Web服务 以下是

我目前正在开发一些javascript,这些javascript可以包含在使用TrueSample的调查的标题中,并将为调查动态生成和激发Webservice调用。Truesample的一个要求是,在每个页面之后,都会发送在该页面上花费的时间,以及在调查开始时生成的一些其他任意信息。我正在尝试自动化每个页面的web服务调用,这样我就不必在每次调查中都有数百个web服务

我已经走了很长的路,并且已经找到了一些很酷的技巧来让这一切正常工作,但是我正在努力使用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);

});
有没有人有使用Javascript启动webservice调用的经验?如果是这样,您对如何完成ajax请求并使其工作有什么想法吗?或者有没有其他(可能更好的)方法可以用于这些调用?我知道有关于堆栈溢出的信息,但我很难理解特定用例如何应用于我的


另外,请注意,虽然我很喜欢使用JQuery,但我仅限于vanilla Javascript和Prototype.JS

使用传统的javascript XmlHttpRequest,您可以进行AJAX调用。对于Web服务,我们需要两个HTTP头。例如:SOAPAction、内容类型、Accept。这些标题的值必须如下所示:

SOAPAction:""
Content-Type:text/xml
Accept:text/xml
因此,另外,在对Web服务进行AJAX调用时,您的代码应该如下所示:

//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
}
}

这对于我想要达到的目标来说是完美的,非常感谢!接下来,由于它是一个独立的域Web服务,并且我无法访问目标Web服务以允许源域访问,因此我似乎在使用它时遇到了问题。我在这里读了第二个答案,它似乎涵盖了我的情况,但我在理解它到底应该如何工作时遇到了一些困难。如果您能提供任何信息,我将不胜感激。请注意,经过几个小时的修补,我终于可以让它正常工作了。再次感谢你的帮助