【发布时间】:2010-02-02 14:52:35
【问题描述】:
我尝试使用 json 调用 web 服务,当我调用没有异常参数的 web 服务时,它的工作, 但是当我尝试发送参数时,我得到了一个错误:
这是我的代码:
function GetSynchronousJSONResponse(url, postData)
{
var xmlhttp = null;
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else if (window.ActiveXObject) {
if (new ActiveXObject("Microsoft.XMLHTTP"))
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
else
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
url = url + "?rnd=" + Math.random(); // to be ensure non-cached version
xmlhttp.open("POST", url, false);
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlhttp.send(postData);
var responseText = xmlhttp.responseText;
return responseText;
}
function Test()
{
var result = GetSynchronousJSONResponse('http://localhost:1517/Mysite/Myservice.asmx/myProc', '{"MyParam":"' + 'test' + '"}');
result = eval('(' + result + ')');
alert(result.d);
}
这是错误:
System.InvalidOperationException:请求格式无效:application/json; charset=utf-8.
在 System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
在 System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest
怎么了?
提前致谢。
【问题讨论】:
-
行 `url = url + "?rnd=" + Math.random();` 是不必要的,
POST请求永远不会被缓存。 -
@Andy E:
POST请求永远不会按标准缓存,我们都知道一些用户代理对标准的重视程度很低。我不会说出任何名字(幸运的是,通常会想到的那个没有受到这个问题的困扰)。 -
@Andrew Moore:我必须说我不知道,但我只使用 Chrome 和 IE。仍然在
POST请求将被缓存的情况下,时间戳可以设置在POST数据中或更合适的if-modified-since请求标头可以设置。 -
@Andy E:罪魁祸首通常使用 URL 的哈希版本进行缓存。我想到了一些移动浏览器。因此,不幸的是,唯一可行的解决方案是
GET参数。 -
@Andrew Moore:很好,我的立场是正确的,我不会再对此事多说 :-)
标签: javascript webservice-client