2010-12-21 60 views
6

我已經完成了大量的閱讀工作,它看起來非常簡單。我建立了我的服務,這是非常簡單的(看起來像這樣從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> 

所有看起來非常簡單,似乎它應該工作,但當我點擊AddSkill時,Chrome瀏覽器JavaScript控制檯返回一個404錯誤,所以它根本找不到服務(我打開了控制檯,因爲當我點擊按鈕時什麼都沒有發生)。

我失去了一些東西在這裏?

順便說一句,我也試過這個(因爲這是在的web.config文件名)

url: "WeddingPhotographer.WeddingPhotographerService.svc/AddNewSkill" 

而且我仍然得到未找到資源(404)錯誤

+0

如果您的「WeddingPhotographerService.svc」位於其他項目中,您將如何調用此功能?我有這種情況,我希望你能幫助我。 – fiberOptics 2012-01-05 08:55:12

回答

4

解決它改變了jQuery的AJAX調用網址

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

,一切都很好