我尝试使用 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;字符集=utf-8。
在 System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
在 System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest
怎么了 ?
提前致谢 。