我遇到的情况是我通过Ajax发送带有字符串数组的SOAP请求,并且请求成功传递给Web服务,但是desearialized数组为空。
我有ASP.Net WebService,签订以下合同:
[WebService(Namespace = "http://someCompany.com/WebServices/TCWS/Agent")]
public class AgentService : WebService
使用以下API:
[WebMethod(Description = "my action description", MessageName = "MyAction")]
public Result MyAction(string[] items)
我使用以下代码通过AJAX(Javascript)发出请求:
AgentService.prototype.DoSoapAjax = function (methodName, data, successHandler, errorHandler, state) {
// Need to use local variable so ajax request will be able to process the guid variable
var currGuid = window.AgentGUID;
var response = $.ajax({
type: "POST",
contentType: "text/xml; charset=utf-8",
methodName: methodName,
url: this.AgentServiceUrl,
data: data,
async: this.Async,
beforeSend: function (req, methodName) {
var soapActionURI = "http://someCompany.com/WebServices/TCWS/Agent/" + this.methodName;
req.setRequestHeader("SOAPAction", soapActionURI);
req.setRequestHeader("AgentGUID", currGuid);
},
success: function (xml, textStatus) {
if (successHandler != null) {
var response = xml.lastChild.firstChild.firstChild.firstChild;
successHandler(state, $.xml2json(response), textStatus);
}
},
error: function (xmlHttpRequest, textStatus, errorThrown) { if (errorHandler != null) errorHandler(state, xmlHttpRequest, textStatus, errorThrown); }
});
if (!this.Async) {
var syncResponse = new Object();
syncResponse.response = response;
syncResponse.json = $.xml2json(response.responseText);
return syncResponse;
}
return response;
};
这就是我对DoSoapAjax的评价:
AgentService.prototype.MyAction= function (items, successHandler, errorHandler, state) {
var items = "<items arrayType='string[]'><item xsi:type='string'>first text</items><item xsi:type='string'>second text</items></items>"
var methodName = "MyAction ";
var soapXml = "<?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/'><soap:Body>"
soapXml += "<" + methodName + " xmlns='http://someCompany.com/WebServices/TCWS/Agent'>";
soapXml += items;
soapXml += "</" + methodName + "></soap:Body></soap:Envelope>";
var response = this.DoSoapAjax("MyAction", soapXml, successHandler, errorHandler, state);
return response;
};
SOAP请求如下:
<?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/'>
<soap:Body>
<MyAction xmlns='http://someCompany.com/WebServices/TCWS/Agent'>
<items xsi:type='string'>first text</items>
<items xsi:type='string'>second text</items>
</MyAction>
</soap:Body>
</soap:Envelope>
当我调试Web服务时,我可以看到我得到MyAction方法但是items是一个空数组而不是带有2个字符串的数组...
我的SOAP请求有问题吗?
答案 0 :(得分:0)
数据对象不是作为字符串数组传递的,而是作为单独的字符串参数添加到正文中。正确的SOAP应该如下所示:
<?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/'>
<soap:Body>
<MyAction xmlns='http://CosmoCom.com/WebServices/TCWS/Agent'>
<items arrayType="string[]">
<item xsi:type='string'>first text</items>
<item xsi:type='string'>second text</items>
</items>
</MyAction>
</soap:Body>
</soap:Envelope>
我认为将数据转换为可以使用的数组
var itemArray = jQuery.makeArray(data);
答案 1 :(得分:0)
我找到了一些解决方案但是我不明白为什么它有效,因为我找不到任何文件。
我已将SOAP请求更改为以下内容:
<?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/'>
<soap:Body>
<MyAction xmlns='http://someCompany.com/WebServices/TCWS/Agent'>
<items>
<string>first text</string>
<string>second text</string>
</items>
</MyAction>
</soap:Body>
</soap:Envelope>
有人可以发布有关的说明或文件,以便更清楚吗?看起来当我传递元素时,我将其名称,当它是数组时,我将其类型。我在一些小型谷歌文档中找到了解决方案...
谢谢大家