如何使用SOAP在javascript AJAX中使用一个代理,与WCF测试客户端使用相同

如何使用SOAP在javascript AJAX中使用一个代理,与WCF测试客户端使用相同,javascript,ajax,wcf,soap,proxy,Javascript,Ajax,Wcf,Soap,Proxy,我有WCF web服务和javascript客户端,它使用SOAP 1.2通过AJAX连接到该服务。我想做的是传递一些参数,告诉AJAX SOAP调用只使用一个代理,就像我在WCF测试客户机中通过取消选中“启动一个新代理”所做的一样。 这是我的SOAP AJAX调用: DoSoapAjax: function (soapMethodName, data, successHandler, errorHandler, isAsync, currentInstance) { var serv

我有WCF web服务和javascript客户端,它使用SOAP 1.2通过AJAX连接到该服务。我想做的是传递一些参数,告诉AJAX SOAP调用只使用一个代理,就像我在WCF测试客户机中通过取消选中“启动一个新代理”所做的一样。

这是我的SOAP AJAX调用:

DoSoapAjax: function (soapMethodName, data, successHandler, errorHandler, isAsync, currentInstance) {
    var service = this;
    var soapResult    = soapMethodName + "Result";
    var soap12WithWsHttpBindRequest ='<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">' +
                      '<s:Header>' +
                      '<a:Action s:mustUnderstand="1">' + this.serviceContractNamespace + '/' + this.interfaceName + '/' + soapMethodName + '</a:Action>' +
                      '<a:MessageID>urn:uuid:605ea0c6-d09b-46bf-b61d-e61b377a135b</a:MessageID>' +
                      '<a:ReplyTo>' +
                      '<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>' +
                      '</a:ReplyTo>' +
                      '<a:To s:mustUnderstand="1">' + this.tenantAdminService + '</a:To>' +
                      '</s:Header>' +
                      '<s:Body>';
                      if (data == emptyString)
                      {
                        soap12WithWsHttpBindRequest +=
                      '<' + soapMethodName + ' xmlns="' + this.serviceContractNamespace + '" />';
                      }
                      else
                      {
                        soap12WithWsHttpBindRequest +=
                      '<' + soapMethodName + ' xmlns="' + this.serviceContractNamespace + '">' +
                      data +
                      '</' + soapMethodName + '>';
                      }
                       soap12WithWsHttpBindRequest +=
                      '</s:Body>' +
                      '</s:Envelope>';
    // in order for ajax to work on jQuery 1.8.2 we need to enable the following.
    // found this answer on the link : http://stackoverflow.com/questions/9160123/no-transport-error-w-jquery-ajax-call-in-ie
    $.support.cors = true;
    // variable to save successData
    var responseData = null;
    // SOAP 1.2 query
    var response = $.ajax({
              type: "POST",
              url: this.tenantAdminService,
              data: soap12WithWsHttpBindRequest,
              contentType: "application/soap+xml",
              dataType: "xml",
              processData: false,
              async: isAsync,
              success: function (data, status, xmlHttpRequest) {
                responseData = data;
                // inserting all data results into dictionary
                var responseResults = {};
                // delegating success function
                if (successHandler != null)
                {
                    responseResults = service.ParseResponse(soapMethodName, data);
                    successHandler(responseResults, currentInstance);
                }                
              },
              error: function (xmlHttpRequest, textStatus, errorThrown) {
                  if (errorHandler != null)
                  {
                    errorHandler(xmlHttpRequest, textStatus, errorThrown, currentInstance);
                  }
                  else if (!isAsync)
                  {
                    alert("Error : " + errorThrown);
                    alert("Error Description : " + xmlHttpRequest.responseText);
                  }

                  return;
              }
          });

        if (!isAsync)
        {   
            return service.ParseResponse(soapMethodName, response.responseXML);
        }
    }  
DoSoapAjax:function(soapMethodName、数据、successHandler、errorHandler、isAsync、currentInstance){
var服务=这个;
var soapResult=soapMethodName+“Result”;
var soap12WithWsHttpBindRequest=“”+
'' +
''+this.serviceContractNamespace+'/'+this.interfaceName+'/'+soapMethodName+''+
'urn:uuid:605ea0c6-d09b-46bf-b61d-e61b377a135b'+
'' +
'http://www.w3.org/2005/08/addressing/anonymous' +
'' +
''此为+this.tenantAdminService+''+
'' +
'';
如果(数据==清空字符串)
{
SOAP12带WSHttpBindRequest+=
'';
}
其他的
{
SOAP12带WSHttpBindRequest+=
'' +
资料+
'';
}
SOAP12带WSHttpBindRequest+=
'' +
'';
//为了让ajax在jQuery1.8.2上工作,我们需要启用以下功能。
//在链接上找到此答案:http://stackoverflow.com/questions/9160123/no-transport-error-w-jquery-ajax-call-in-ie
$.support.cors=true;
//变量来保存successData
var responseData=null;
//SOAP1.2查询
var响应=$.ajax({
类型:“POST”,
url:this.tenantAdminService,
数据:SOAP12 WithWSHttpBindRequest,
contentType:“应用程序/soap+xml”,
数据类型:“xml”,
processData:false,
异步:isAsync,
成功:函数(数据、状态、xmlHttpRequest){
响应数据=数据;
//将所有数据结果插入字典
var responseResults={};
//授权成功功能
if(successHandler!=null)
{
responseResults=service.ParseResponse(soapMethodName,数据);
successHandler(responseResults,currentInstance);
}                
},
错误:函数(xmlHttpRequest、textStatus、errorshown){
if(errorHandler!=null)
{
errorHandler(xmlHttpRequest、textStatus、ErrorSprown、currentInstance);
}
如果(!isAsync)
{
警报(“错误:+错误抛出”);
警报(“错误描述:+xmlHttpRequest.responseText”);
}
返回;
}
});
如果(!isAsync)
{   
返回service.ParseResponse(soapMethodName,response.responseXML);
}
}  

首先,我会说我不知道这个问题的具体答案,但我的方法是嗅探来自WFC测试客户端的流量,看看它是否向调用添加了任何参数,您可以在AJAX调用中向请求头添加这些参数

Fiddler可能会完成这项工作,但我不确定它是否能捕获所有http流量——主要是针对浏览器的。还有一些其他工具或多或少具有功能和易用性,如wire sharkEthernet

我猜在SOAP信封主体之外还有一些其他的请求参数从测试客户机传输过来,您应该能够看到。这是我能想象的测试客户机能够在不修改SOAP消息的情况下进行通信的唯一方法

如果可以找到名称和值,可以将其作为另一个名称值JSON实体添加到data:parameter中。

liorafar: 我假设您是从作为主机的浏览器运行Javascript。如果是这样的话,除非您允许javascript更改主机的某些行为,这是不可能的,因为对于浏览器识别为安全风险的每一个执行,客户端都会批准该行为。 认为: 1) Javascript不控制连接配置,它只使用运行它的主机提供的环境,在这种情况下是浏览器,但也可以在服务器端。 2) 如果你说“好吧,让我们来强制这个”,我想到的唯一方法就是创建一个你的主机或客户端主机的任何对象的实例,并强制更改连接。。。到那一刻,浏览器将检测到你的javascript存在安全风险,因此你应该为市场上的每一款浏览器创建一个脚本,因为每一款浏览器都有自己的处理方法。。。这方面唯一的好消息是,使用IE,您也可以更改Chrome的配置

为了解决这个问题,我将要求IT人员对浏览器进行配置,并对浏览器中的代理进行一些例外规则(如果您是一家公司的员工)

我希望这能在某种程度上有所帮助


Gabriel

您可以将soap请求放入Fiddler中,然后执行,您将得到响应。对于该响应类型。当您收到响应时,请在您想要的模式下查看。

这是您自己的服务器还是第三方SOAP服务?由于跨站点问题,我想知道客户机是否真的通过此函数将xml发布到第三方SOAP ws。我相信大多数浏览器不允许您从原始网页直接通过js向第三方发布xml