我在 _Layout.cshtml 中有一个 ajax 调用。
<script lang="javascript" type="text/javascript">
function ttsFunction() {
serviceUrl = "http://localhost:8080/wscccService.svc/RunTts";
var data = new Object();
data.text = $('#speak').val();
var jsonString = JSON.stringify(data);
$.ajax({
type: 'POST',
url: serviceUrl,
data: jsonString,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
error: function (xhr,status,error) {
console.log("Status: " + status);
console.log("Error: " + error);
console.log("xhr: " + xhr.readyState);
},
statusCode: {
404: function() {
console.log('page not found');
}
}
});
}
</script>
基本上我想单击另一个视图中的按钮来调用此服务。Community.csthml 中的按钮是:
<button id="btnSpeak" onclick="ttsFunction();">Speak</button>
当我在浏览器中输入 svc 路径时,在 IISEXPRESS 下的 Tracelog 文件夹中发现异常。
例外是:
[HttpException]: The controller for path &#39;/wscccService.svc/&#39; was not found or does not implement IController.
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
--></Data>
服务本身很简单:
namespace service
{
[ServiceContract]
public interface Iwservice
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "RunTts")]
string RunTts(string text);
}
}
为了节省空间,除非您需要,否则我不会发布 web.config。感谢帮助。