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

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

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

 1   httpRequest.open("GET", "WebServices.asmx/GetUploadStatus", true);
 2                     //httpRequest.setRequestHeader("If-Modified-Since","0"); 
 3                     httpRequest.send();
 4                     httpRequest.onreadystatechange = function () {
 5                         if (httpRequest.readyState == 4 && httpRequest.status == 200) {
 6                             var resultValue = httpRequest.responseText;
 7                             //表示初始化上传时,显示进度是0%
 8                             if (resultValue == "") {
 9                                 completeResult.innerHTML = "上传进度:0%";
10                                 return;
11                             }
12                             //设置进度条
13                             bar.style.width = 2 * resultValue + "px";
14 
15                             completeResult.innerHTML = "上传进度:" + resultValue + "%";
16                             //当result为100时,不再进行进度条的更新
17                             if (resultValue == 100) {
18                                 //自动消失
19                                 window.clearInterval(vailed);
20                                 completeResult.innerHTML = "上传进度:100% " + "上传已完成!";
21                             }
22                         }
23                     }
24                 }, 400);
调用webservice访问服务器并获取返回信息

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

1       <webServices>
2           <protocols>
3               <add name="HttpGet" />
4               <add name="HttpPost" /> 
5               <add name="Unknown" />
6               <add name="HttpSoap" />
7           </protocols>
8       </webServices>
9   </system.web>
配置webservice节点

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

最后贴上webservice部分代码

 1  //获取上传文件信息类(从Session中取出来)
 2             UploadInfo upload = RequestUploadFiles() as UploadInfo;
 3             //若对象不为空,并且已经准备好  
 4             if (upload != null && upload.IsReady)
 5             {
 6                 long uploadedSize = upload.UploadedLength;   //已上传大小
 7                 long total = upload.ContentLength;           //上传文件总大小  
 8                 //将其转化为百分比  
 9                 float percentComplete = (float)uploadedSize / (float)total * 100;
10 
11                 HttpContext.Current.Response.Write(percentComplete.ToString("F2")); //保留两位小数  
12             }
13             else
14             {
15                 //还没有准备好上传文件  
16                 HttpContext.Current.Response.Write("10");
17             }
和本问题相关的webservice(GetUploadStatus函数)部分代码
原文地址:https://www.cnblogs.com/AlphaThink-AT003/p/3880076.html