无法从 JQuery 调用 Webmethod

发布于 2024-09-30 20:57:15 字数 93 浏览 0 评论 0原文

我无法从 jquery 调用 webmethod。我认为它与 web.config 文件有关。如何为 Web 服务和 webmethod 设置 web.config 文件?

I cannot call webmethod from jquery. I think its about web.config file. How can i set web.config file for web services and webmethod?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

書生途 2024-10-07 20:57:15

你有这个代码吗?

        $.ajax({
            url: "Services/MyService.svc/Service",
            type: "GET",
            context: document.body,
            contentType: 'application/json; charset=utf-8',
            datatype: 'json',
            success: function (data) {
                // do something
            }
        });

请注意,contentType 部分至关重要。

如果这样做,请检查 Firebug 中“网络”选项卡中抛出的确切错误。
通常,人们会遇到不同的问题,具体取决于服务类型 - ASP.NET asmx 与 WCF svc。关于asmx配置,请参考如何让ASMX文件输出JSON。对于 wcf,您需要设置 web.config 以允许 Web 脚本编写,如下所示:

<system.serviceModel>
 <behaviors>
  <endpointBehaviors>
    <behavior name="AspNetAjaxBehavior">
      <enableWebScript/>
    </behavior>
  </endpointBehaviors>
 </behaviors>

然后配置服务以使用该行为:

  <services>
    <service name="MyProject.Services.MyService">
      <endpoint address="/Services/MyService.svc" behaviorConfiguration="AspNetAjaxBehavior" binding="webHttpBinding" contract="MyProject.Services.MyService"/>
    </service>
  </services>
</system.serviceModel>

Do you have this code?

        $.ajax({
            url: "Services/MyService.svc/Service",
            type: "GET",
            context: document.body,
            contentType: 'application/json; charset=utf-8',
            datatype: 'json',
            success: function (data) {
                // do something
            }
        });

note that contentType part is critical.

If you do, check Firebug for exact error that gets thrown in the "Net" tab.
Normally, people have different problems depending on service type - ASP.NET asmx vs. WCF svc. For asmx configuration, refer to How to let an ASMX file output JSON . For wcf, you need to set up web.config to allow web scripting, like so:

<system.serviceModel>
 <behaviors>
  <endpointBehaviors>
    <behavior name="AspNetAjaxBehavior">
      <enableWebScript/>
    </behavior>
  </endpointBehaviors>
 </behaviors>

and then later configure the service to use that behavior:

  <services>
    <service name="MyProject.Services.MyService">
      <endpoint address="/Services/MyService.svc" behaviorConfiguration="AspNetAjaxBehavior" binding="webHttpBinding" contract="MyProject.Services.MyService"/>
    </service>
  </services>
</system.serviceModel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文