【发布时间】:2015-09-08 15:40:35
【问题描述】:
我有一种情况,我通过 Ajax 发送带有字符串数组的 SOAP 请求,请求成功传递给 Web 服务,但是反序列化的数组为空。
我的 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 请求有问题吗?
【问题讨论】:
-
在我看来您传递的是多个字符串参数而不是字符串数组
-
你怎么打电话给
DoSoapAjax?你能发布那个代码吗? -
@lc。添加了DoSoapAjax的调用函数
标签: javascript c# ajax web-services soap