使用Ajax调用WCF

系统教程 行业动态 更新时间:2024-06-14 16:58:30
使用Ajax调用WCF - 404错误(Calling WCF with Ajax - 404 Error)

我一直在尝试关注本文并创建一个WCF服务以通过Ajax调用进行访问。 我在Ajax调用上得到了404,并且不知道为什么因为它对我来说没问题。 有人可以帮我修复404错误并告诉我我在哪里有命名空间或者有什么不对吗?

在有人对我大喊大叫之前,我从未创建过WCF服务(更不用说从Ajax调用它了)所以我正在关注这篇文章,其中说现在不要担心界面:)

namespace SearchService { [ServiceContract] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class Search { [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] public string SearchFiles(string searchText) { StringBuilder result = new StringBuilder(); try { string currentFolder = Path.GetDirectoryName(@"C:\somefolder\views\filteredViews\"); string[] files = Directory.GetFiles(currentFolder, "*.htm"); foreach (string file in files) { string contents = File.ReadAllText(file); if (contents.Contains(searchText)) { result.AppendLine(file); result.Append("|"); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return result.ToString(); } } } <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="SearchServiceBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="webEndPointBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <services> <service name="SearchService.Search" behaviorConfiguration="SearchServiceBehavior"> <endpoint address="" contract="SearchService.Search" binding="webHttpBinding" behaviorConfiguration="webEndPointBehavior" name="webEndPoint" /> </service> </services> </system.serviceModel> var Type; var Url; var Data; var ContentType; var DataType; var ProcessData; $("#searchButton").click(function () { var searchText = $("#searchText").val(); Type = "POST"; Url = "Search.svc/SearchFiles"; Data = '{"' + searchText + '"}'; ContentType = "application/json; charset=utf-8"; DataType = "json"; varProcessData = true; CallService(); }); // Function to call WCF Service function CallService() { $.ajax({ type: Type, //GET or POST or PUT or DELETE verb url: Url, // Location of the service data: Data, //Data sent to server contentType: ContentType, // content type sent to server dataType: DataType, //Expected data format from server processdata: ProcessData, //True or False success: function(msg) { //On Successfull service call ServiceSucceeded(msg); }, error: ServiceFailed // When Service call fails }); } function ServiceFailed(result) { alert('Service call failed: ' + result.status + '' + result.statusText); Type = null; varUrl = null; Data = null; ContentType = null; DataType = null; ProcessData = null; } function ServiceSucceeded(result) { if (DataType == "json") { alert("It worked" + result); } } function ServiceFailed(xhr) { if (xhr.responseText) { var err = xhr.responseText; if (err) error(err); else error({ Message: "Unknown server error." }) } return; }

I've been trying to follow this article and create a WCF Service to access with an Ajax call. I am getting a 404 on my Ajax call and have no idea why because it looks ok to me. Can someone please help me fix the 404 error and show me where I have a namespace or whatever wrong?

Before anyone yells at me, I have never created a WCF service (let alone call it from Ajax) so I am following the article, which says do not worry about an interface right now :)

namespace SearchService { [ServiceContract] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class Search { [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] public string SearchFiles(string searchText) { StringBuilder result = new StringBuilder(); try { string currentFolder = Path.GetDirectoryName(@"C:\somefolder\views\filteredViews\"); string[] files = Directory.GetFiles(currentFolder, "*.htm"); foreach (string file in files) { string contents = File.ReadAllText(file); if (contents.Contains(searchText)) { result.AppendLine(file); result.Append("|"); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return result.ToString(); } } } <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="SearchServiceBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="webEndPointBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <services> <service name="SearchService.Search" behaviorConfiguration="SearchServiceBehavior"> <endpoint address="" contract="SearchService.Search" binding="webHttpBinding" behaviorConfiguration="webEndPointBehavior" name="webEndPoint" /> </service> </services> </system.serviceModel> var Type; var Url; var Data; var ContentType; var DataType; var ProcessData; $("#searchButton").click(function () { var searchText = $("#searchText").val(); Type = "POST"; Url = "Search.svc/SearchFiles"; Data = '{"' + searchText + '"}'; ContentType = "application/json; charset=utf-8"; DataType = "json"; varProcessData = true; CallService(); }); // Function to call WCF Service function CallService() { $.ajax({ type: Type, //GET or POST or PUT or DELETE verb url: Url, // Location of the service data: Data, //Data sent to server contentType: ContentType, // content type sent to server dataType: DataType, //Expected data format from server processdata: ProcessData, //True or False success: function(msg) { //On Successfull service call ServiceSucceeded(msg); }, error: ServiceFailed // When Service call fails }); } function ServiceFailed(result) { alert('Service call failed: ' + result.status + '' + result.statusText); Type = null; varUrl = null; Data = null; ContentType = null; DataType = null; ProcessData = null; } function ServiceSucceeded(result) { if (DataType == "json") { alert("It worked" + result); } } function ServiceFailed(xhr) { if (xhr.responseText) { var err = xhr.responseText; if (err) error(err); else error({ Message: "Unknown server error." }) } return; }

最满意答案

好吧,似乎我的服务设置正确。 当我试图从Ajax调用它时出现问题。 似乎不是把它放在Ajax调用中:

Url = "Search.svc/SearchFiles";

我需要在Ajax调用中使用它:

url: "http://localhost:57761/Search.svc/SearchFiles"

除非我在搜索中错过了这个(或者我仍然有点错误),所以只需要这样。

如果有人关心进一步解释(或特别添加解释差异的链接),请做。

Well, it seems that my service was setup correctly. The problem arose when I tried to call it from Ajax. It seems that instead of putting this in the Ajax call:

Url = "Search.svc/SearchFiles";

I needed to have this in the Ajax call:

url: "http://localhost:57761/Search.svc/SearchFiles"

Unless I missed this in my searching (or I am sort of still wrong), that is all it took.

If anyone cares to explain this further (or especially add a link that explains the difference), please do.

更多推荐

本文发布于:2023-04-15 03:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/ebca727b001add37a81a3a2a9ae48eee.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Ajax   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!