从 jQuery 调用启用 AJAX 的 WCF 服务 - MVC 2

发布于 2024-10-08 13:17:51 字数 2641 浏览 1 评论 0原文

我已经阅读了大量关于实现这一目标的书籍,看起来非常简单。我创建了我的服务,非常简单(看起来很

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WeddingPhotographerService
{    
    // Add more operations here and mark them with [OperationContract]
    [OperationContract]
    public bool AddNewSkill(string name, string description)
    {
        IRepository<Skill> skillRepo = ObjectFactory.GetInstance<IRepository<Skill>>();

        var skill = new Skill { Name = name, Description = description };
        skillRepo.Save(skill);
        return true;
    }
}

简单,然后我在我的视图中编写了这个 jQuery 代码。

$(document).ready(function () {
    $("#AddSkill").click(function () {
        var data = { name: $("#NewSkill").val(), description: "" };
        data = JSON.stringify(data)
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "WeddingPhotographerService.svc/AddNewSkill",
            data: data,
            dataType: "json",
            success: function () {
                $('#SkillListViewContainer').load('../AccountController/GetSkillControl');
            },
            error: function (msg) {
                $("#AddSkillError").text(msg.d);
            }
        });
    });
});

我的 WeddingPhotographerService.svc 位于项目的根目录中,即 web.config在我创建服务时添加了这个

<system.serviceModel>
  <behaviors>
    <endpointBehaviors>
      <behavior name="WeddingPhotographer.WeddingPhotographerServiceAspNetAjaxBehavior">
        <enableWebScript />
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <services>
    <service name="WeddingPhotographer.WeddingPhotographerService">
      <endpoint address="" behaviorConfiguration="WeddingPhotographer.WeddingPhotographerServiceAspNetAjaxBehavior"
        binding="webHttpBinding" contract="WeddingPhotographer.WeddingPhotographerService" />
    </service>
  </services>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
    multipleSiteBindingsEnabled="true" />
</system.serviceModel>

所有看起来都很简单并且看起来应该可以工作,但是当我单击 Chrome JavaScript 控制台的 AddSkill 时,会返回 404 错误,因此它根本找不到该服务(我打开控制台,因为当我单击按钮时什么也没有发生)。

顺便

说一句,我也尝试过这个(因为这是 web.config 文件中的名称)。

url: "WeddingPhotographer.WeddingPhotographerService.svc/AddNewSkill"

我仍然收到资源未找到 (404) 错误

I've done a ton of reading on accomplishing this and it seems pretty straightforward. I created my service, which is very simple (looks like this

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WeddingPhotographerService
{    
    // Add more operations here and mark them with [OperationContract]
    [OperationContract]
    public bool AddNewSkill(string name, string description)
    {
        IRepository<Skill> skillRepo = ObjectFactory.GetInstance<IRepository<Skill>>();

        var skill = new Skill { Name = name, Description = description };
        skillRepo.Save(skill);
        return true;
    }
}

Simple enough right, then I write up this jQuery code in my view

$(document).ready(function () {
    $("#AddSkill").click(function () {
        var data = { name: $("#NewSkill").val(), description: "" };
        data = JSON.stringify(data)
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "WeddingPhotographerService.svc/AddNewSkill",
            data: data,
            dataType: "json",
            success: function () {
                $('#SkillListViewContainer').load('../AccountController/GetSkillControl');
            },
            error: function (msg) {
                $("#AddSkillError").text(msg.d);
            }
        });
    });
});

My WeddingPhotographerService.svc is in the root of the project, the web.config added this when I created the service

<system.serviceModel>
  <behaviors>
    <endpointBehaviors>
      <behavior name="WeddingPhotographer.WeddingPhotographerServiceAspNetAjaxBehavior">
        <enableWebScript />
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <services>
    <service name="WeddingPhotographer.WeddingPhotographerService">
      <endpoint address="" behaviorConfiguration="WeddingPhotographer.WeddingPhotographerServiceAspNetAjaxBehavior"
        binding="webHttpBinding" contract="WeddingPhotographer.WeddingPhotographerService" />
    </service>
  </services>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
    multipleSiteBindingsEnabled="true" />
</system.serviceModel>

All seems simple enough and appears it should work but when I click the AddSkill the Chrome JavaScript console a 404 error is returned, so it's not finding the service at all (I opened up the console because nothing at all was happening when I would click the button).

Am I missing something here?

By the way, I also tried this (since that's the name in the web.config file)

url: "WeddingPhotographer.WeddingPhotographerService.svc/AddNewSkill"

And I still get the Resource Not Found (404) error

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

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

发布评论

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

评论(1

焚却相思 2024-10-15 13:17:51

解决了它,将 jQuery AJAX 调用中的 url 行更改为

url: "../WeddingPhotographerService.svc/AddNewSkill"

一切都很好

Solved it, changed the url line in the jQuery AJAX call to

url: "../WeddingPhotographerService.svc/AddNewSkill"

And all is well

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文