从 jQuery 调用启用 AJAX 的 WCF 服务 - MVC 2
我已经阅读了大量关于实现这一目标的书籍,看起来非常简单。我创建了我的服务,非常简单(看起来很
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了它,将 jQuery AJAX 调用中的 url 行更改为
一切都很好
Solved it, changed the url line in the jQuery AJAX call to
And all is well