这是(希望)一个简单的问题。我必须使用 XDomainRequest 通过 POST 向 Web 服务提交请求。我在互联网上找到了很少的文档,但我拒绝相信没有人知道这一点。
这是我的 XDomainRequest 代码:
var testURL = 'http://localhost:4989/testendpoint';
//let us check to see if the browser is ie. If it is not, let's
if ($.browser.msie && window.XDomainRequest) {
var xdr2 = new XDomainRequest();
xdr2.open("POST", testURL);
xdr2.timeout = 5000;
xdr2.onerror = function () {
alert('we had an error!');
}
xdr2.onprogress = function () {
alert('we have some progress!');
};
xdr2.onload = function () {
alert('we load the xdr!');
var xml2 = new ActiveXObject("Microsoft.XMLDOM");
xml2.async = true;
xml2.loadXML(xdr2.responseText);
};
//what form should my request take to be sending a string for a POST request?
xdr2.send("thisisastring");
}
根据 Web 服务的帮助页面,我的 Web 服务 (WCF) 采用单个参数,如下所示:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">String content</string>
我已经通过其他http客户端(移动和桌面API,提琴手)通过构建一个字符串,将我尝试传递给Web服务的参数与字符串序列化的其余部分连接起来,从而使其工作。例如,我尝试过:
xdr2.send("thisisastring");
xdr2.send("<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">thisisastring</string>");
但是 onerror 处理程序总是被触发。我认为这与 WCF 没有任何关系,因为:
- WCF 在我调用它的所有其他客户端中总是成功的,并且
- 如果是服务,onerror 方法将永远不会被触发。它会返回垃圾,但它会返回一些东西。
当我使用控制台(在 ie9 的开发工具中)记录 responseText 时,它说:
LOG:undefined
所以我相当确定问题在于我如何使用 XDomainRequest。