我一直在尝试关注此article并创建一个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;
}
答案 0 :(得分:0)
好吧,似乎我的服务设置正确。当我试图从Ajax调用它时出现问题。似乎不是把它放在Ajax调用中:
Url = "Search.svc/SearchFiles";
我需要在Ajax调用中使用它:
url: "http://localhost:57761/Search.svc/SearchFiles"
除非我在搜索中错过了这个(或者我仍然有点错误),否则就是这样。
如果有人愿意进一步解释(或者特别添加解释差异的链接),请执行。