代码之家  ›  专栏  ›  技术社区  ›  Remotec

JQuery正在使用ASP.Net Web服务webserver-URL的请求格式意外结束于

  •  0
  • Remotec  · 技术社区  · 15 年前

    在这个问题上做了很多谷歌搜索,但似乎找不到答案。

    当我从Jquery调用我的web服务时,我收到了错误

    Request format is unrecognized for URL unexpectedly ending in '/AirportSearchGeneric'. 
    

    因素

    • 我当前正在调用同一台计算机上但在不同Web服务器上的Web服务(调用应用程序的端口是64004,接收应用程序的端口是1400)-是否可能存在跨“域”问题?两者都是本地主机。

    • 两者都在使用visual studio的测试web服务器。

    • 我已经尝试将这两个协议添加到web.config(add name=“HttpGet”add name=“HttpPost”)

    • 该错误发生在服务器上的事件查看器中。

    • 我在萤火虫里得到了以下信息。。。

      选项AirportSearchGeneric http://localhost:1400/services/airportservice.asmx/AirportSearchGeneric

      500内部服务器错误

      本地主机:1400

    ... 以前未看到选项,但正在使用POST请求访问请求。

    JQuery代码。。。

    $.ajax({
                        type: "POST",
                        url: "http://localhost:1400/services/airportservice.asmx/AirportSearchGeneric",
                        data: "{'criteria':'EGBB', 'maxResults':'10'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (msg) {
                            alert(msg.d);
                        }
                    });
    

    Web服务代码。。。

    [WebService(Namespace = "http://localhost/WebServices")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ScriptService]
        public class AirportService : WebService
        {
            [WebMethod]
            [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
            public string AirportSearchGeneric(string criteria, int maxResults)
            {
                IAirportService svc = new Airports.AirportService.AirportService();
                List<AirportSearchResult> res = svc.AirportSearchGeneric(criteria, maxResults);
    
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(res.GetType());
                MemoryStream ms = new MemoryStream();
                serializer.WriteObject(ms, res);
                string jsonString = Encoding.Default.GetString(ms.ToArray());
                ms.Close();
                return jsonString;
            }
        }
    

    ... 不要认为这里有问题,因为当调试时,这里没有代码被执行。

    我很确定我已经涵盖了所有我读到的关于为什么会发生这种情况的原因,所以对于我如何使这种工作的任何建议都是非常有用的。

    干杯。

    作为参考,firebug头如下所示:

    Host    localhost:1400
    User-Agent  Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 ( .NET CLR 3.5.30729; .NET4.0E)
    Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language en-gb,en;q=0.5
    Accept-Encoding gzip,deflate
    Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive  115
    Connection  keep-alive
    Origin  http://localhost:64004
    Access-Control-Request-Me...    POST
    

    (firebug中除了500个错误没有收到响应,根本没有html响应)。

    1 回复  |  直到 15 年前
        1
  •  1
  •   Community Mohan Dere    9 年前

    在同一台计算机上使用不同的端口被认为是跨域的,浏览器不允许使用,因为您怀疑。

    您可以通过使用JSONP欺骗浏览器来调用另一个端口(如果可以限制自己只使用GET请求),或者将其中一个端口更改为与另一个相同。

    Ajax Cross Domain Calls