仍然得到“请求格式无法识别URL意外结束...”

时间:2011-03-27 22:46:03

标签: jquery asp.net ajax web-services

问候,

我到处寻找,我看到的每个答案都告诉我将以下内容放在我的web.config中:

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

所以我已经做到了,我仍然得到错误。

以下是我网页上的脚本:

<script>
    $(function () {
        $('#btn').click(
            function () {
                CallHome();
            }
        );
    });

    function CallHome() {
        $.ajax({
            type: "POST",
            url: "HelloWorld.asmx/HelloWorld",
            dataType: "xml",
            data: "{}",
            contentType: "xml",
            success: function(data) {
                alert(data);
            }
        });
    }
</script>

WebService.vb是:

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Web.Script.Services

<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class HelloWorld
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "Hello World"
    End Function

End Class

我创建了一个全新的网络项目,以减少我的榜样。在某个地方,我搞砸了!你能帮忙吗?

谢谢!

更新:对小提琴手来说是新手......但这里是ajax帖子的标题:

OPTIONS http://localhost:49891/HelloWorld.asmx/HelloWorld HTTP/1.1
Host: localhost:49891
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: null
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16
Access-Control-Request-Headers: Content-Type, Accept
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

以下是我正常形式帖子(有效)时的标题:

POST http://localhost:49891/HelloWorld.asmx/HelloWorld HTTP/1.1
Host: localhost:49891
Connection: keep-alive
Referer: http://localhost:49891/HelloWorld.asmx?op=HelloWorld
Content-Length: 0
Cache-Control: max-age=0
Origin: http://localhost:49891
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16
Content-Type: application/x-www-form-urlencoded
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: OriginalGUID=2d9e0303-754d-4871-a875-9e184701e91d

不确定这是否有帮助...

更新2: 这是来自服务器的错误响应:

HTTP/1.1 500 Internal Server Error
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 28 Mar 2011 05:06:18 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 4444
Connection: Close

<html>
    <head>
        <title>Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.</title>
        <style>
         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
         pre {font-family:"Lucida Console";font-size: .9em}
         .marker {font-weight: bold; color: black;text-decoration: none;}
         .version {color: gray;}
         .error {margin-bottom: 10px;}
         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
        </style>
    </head>

    <body bgcolor="white">

            <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>

            <h2> <i>Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.</i> </h2></span>

            <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">

            <b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

            <br><br>

            <b> Exception Details: </b>System.InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.<br><br>

            <b>Source Error:</b> <br><br>

            <table width=100% bgcolor="#ffffcc">
               <tr>
                  <td>
                      <code>

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</code>

                  </td>
               </tr>
            </table>

            <br>

            <b>Stack Trace:</b> <br><br>

            <table width=100% bgcolor="#ffffcc">
               <tr>
                  <td>
                      <code><pre>

[InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in &#39;/HelloWorld&#39;.]
   System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +518909
   System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +212
   System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) +47
   System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +203
   System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +128
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +184
</pre></code>

                  </td>
               </tr>
            </table>

            <br>

            <hr width=100% size=1 color=silver>

            <b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1

            </font>

    </body>
</html>
<!-- 
[InvalidOperationException]: Request format is unrecognized for URL unexpectedly ending in &#39;/HelloWorld&#39;.
   at System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response)
   at System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath)
   at System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
   at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
   at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
-->

5 个答案:

答案 0 :(得分:2)

我正在寻找这个问题的解决方案并找到了这个问题,虽然我会分享我的解决方法/黑客。我在sharepoint中使用asmx web服务,并且需要asmx方法来正确响应HEAD请求(显然,在执行GET请求下载源之前,itunes存储会向您的RSS源URL发出HEAD请求)。从做一些谷歌搜索我发现asmx web服务不支持HEAD请求,这似乎是正确的http://forums.asp.net/t/1442810.aspx/1

知道asmx和HEAD并不能很好地协同工作,我不想重做WCF中的所有内容,我试图找出阻力最小的黑客。在与团队协商之后,我们决定创建一个HTTP模块,该模块将查找对asmx Web服务的HEAD请求,并继续在服务器上执行GET请求并返回标头。我知道这有点浪费,但它似乎与我在CURL / firebug中的测试所预期的一样好。这是HTTP模块的大部分代码。

public class HeadInterceptor : IHttpModule
{
    if (HttpContext.Current.Request.Url.AbsoluteUri.ToLower().Contains("asmx") 
        && HttpContext.Current.Request.HttpMethod == "HEAD")
    {
        WebRequest req = HttpWebRequest.Create(HttpContext.Current.Request.Url);
        req.Method = "GET";
        System.Net.WebResponse resp = req.GetResponse();
        HttpContext.Current.Response.Clear();
        foreach (string k in resp.Headers.AllKeys)
        {
            HttpContext.Current.Response.AddHeader(k, resp.Headers[k]);
        }
        HttpContext.Current.Response.End();
    }
}

答案 1 :(得分:0)

当您从浏览器点击该服务时,您是否收到“Hello World”?

另外,你在网址中错过了一个尾随的“/”吗?

答案 2 :(得分:0)

您的网络服务和网页是否在同一解决方案中,或者您运行的是两台服务器?当我的网页在另一台服务器上调用Web服务时,我遇到了问题。这会导致Javascript出现一些权限问题。

如果您在其他服务器上运行它,则可能必须在包含该网页的解决方案中创建代理Web服务。此服务需要在您的其他解决方案中调用Web服务。在您的网页上,您可以调用本地Web服务,该服务将您的呼叫重定向到另一台服务器上的呼叫。

答案 3 :(得分:0)

我不想发布我自己并不完全了解的问题的答案,但我最近遇到了这个问题而且我认为我会分享我的相当特殊的解决方案。

我有一个网络服务已经好几个月工作,突然开始抛出“请求格式无法识别......”例外。该服务已经为GET和POST协议正确配置,因此不是问题。我想也许是另一个队友改变了web.config,但事实并非如此。

我偶然遇到的解决方案是在包含webservice的文件夹中删除一个新的空web.config文件,导致无效的配置错误。删除空的web.config文件后,Web服务再次神奇地开始工作。我不明白为什么它首先打破了,我不是100%明确为什么修复它,但确实如此。的 YMMV!

答案 4 :(得分:0)

当然,这篇文章对Dave来说太晚了,但对于得到此消息的人来说可能是有用的。

我在Internet Explorer中测试一个Web服务,每次调用Web服务时都会出现此错误。

在我发现我安装了名为 Lucky Leap 的不需要的应用程序后进行了一些调试。这会为Internet Explorer添加一个触发错误消息的扩展名。

我已将其卸载(控制面板 - &gt;程序(或添加/删除程序) - &gt;卸载程序),并且能够再次无错误地调用我的网络服务。

我希望这有帮助,