WebService "因 URL 意外地以 结束,请求格式无法识别" 的解决方法

时间:2023-03-10 02:33:09
WebService "因 URL 意外地以 结束,请求格式无法识别" 的解决方法

最近在做一个图片上传的功能,js调用用webservice进行异步访问服务器,对于不是经常用webservice的菜鸟来说,经常会遇到以下的问题(起码我是遇到了)

在页面上写了js调用代码如下所示:

   httpRequest.open("GET", "WebServices.asmx/GetUploadStatus", true);
//httpRequest.setRequestHeader("If-Modified-Since","0");
httpRequest.send();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == && httpRequest.status == ) {
var resultValue = httpRequest.responseText;
//表示初始化上传时,显示进度是0%
if (resultValue == "") {
completeResult.innerHTML = "上传进度:0%";
return;
}
//设置进度条
bar.style.width = * resultValue + "px"; completeResult.innerHTML = "上传进度:" + resultValue + "%";
//当result为100时,不再进行进度条的更新
if (resultValue == ) {
//自动消失
window.clearInterval(vailed);
completeResult.innerHTML = "上传进度:100% " + "上传已完成!";
}
}
}
}, );

调用webservice访问服务器并获取返回信息

刚刚完成,webservice并没有配置就执行了,然后结果就是网页一直崩溃,最后通过FireBUg查看了一下 终于恍然大悟,原来webservice没进行配置,于是就在网上去搜解决办法,用了0.1秒的时间,终于找到了。就是配置webconfig.如下代码所示:

       <webServices>
<protocols>
<add name="HttpGet" />
<add name="HttpPost" />
<add name="Unknown" />
<add name="HttpSoap" />
</protocols>
</webServices>
</system.web>

配置webservice节点

配置成功后,程序正常运行。

最后贴上webservice部分代码

  //获取上传文件信息类(从Session中取出来)
UploadInfo upload = RequestUploadFiles() as UploadInfo;
//若对象不为空,并且已经准备好
if (upload != null && upload.IsReady)
{
long uploadedSize = upload.UploadedLength; //已上传大小
long total = upload.ContentLength; //上传文件总大小
//将其转化为百分比
float percentComplete = (float)uploadedSize / (float)total * ; HttpContext.Current.Response.Write(percentComplete.ToString("F2")); //保留两位小数
}
else
{
//还没有准备好上传文件
HttpContext.Current.Response.Write("");
}

和本问题相关的webservice(GetUploadStatus函数)部分代码