【问题标题】:Calling asmx service from JavaScript从 JavaScript 调用 asmx 服务
【发布时间】:2012-03-16 20:33:27
【问题描述】:

我有以下 asmx 服务

[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]

    public class DinnerService : System.Web.Services.WebService
    {
        List<Dinner> dinners;
        public DinnerService()
        {
            dinners = new List<Dinner>();
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public Dinner HelloWorld()
        {
            var dinner = new Dinner { DinnerID = 1, Host = "Ahsan", RSVP = "Some People", Venue = "Lahore" };
            return dinner;
        }
}

我在 webform 的页面加载事件中从 jQuery 调用它,如下所示

    $(function () {
                $.ajax({
                    type: 'post',
                    url: 'http://localhost:1901/DinnerService.asmx/HelloWorld',
                    dataType: 'json',
                    data:{},
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        $('#res').html(data.d.DinnerID);
                        alert(data.d.Host);
                    }
                });
});

它在 IE 上运行良好,但在 Firefox 和 Chrome 中显示内部服务器错误,响应如下:

Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'

这对我来说很奇怪,为什么它可以在一个浏览器上运行,而不能在其他浏览器上运行。我正在为该服务使用 Visual Studio 2010 和 .NET 3.5。对于这种特殊情况,不要选择 WCF。互联网上的回复无法帮助我解决问题。这里到底发生了什么?

【问题讨论】:

  • 你试过这个解决方案了吗? stackoverflow.com/questions/657313/…
  • 网上有很多类似标题的问题,但他们在特定的浏览器中没有遇到问题。此外,我在服务项目的 web.config 中找不到&lt;protocols&gt; 部分
  • 如果您的 web.config 中没有 部分,只需在 部分下添加一个即可。
  • 我做了但无济于事:(

标签: asp.net web-services .net-3.5 asmx


【解决方案1】:

您的 web.config 文件的以下设置将解决该问题。 请参阅follownig 帖子。

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

【讨论】:

    猜你喜欢
    • 2014-02-12
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    相关资源
    最近更新 更多