如何使用ASP.NET AJAX和jQuery AJAX调用服务器端方法(包含文本到语音代码) [英] How to Call Server Side method( which contain text To Speech Code) using ASP.NET AJAX and jQuery AJAX

查看:109
本文介绍了如何使用ASP.NET AJAX和jQuery AJAX调用服务器端方法(包含文本到语音代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我.

我正在写一种将.cs文件中的文本转换为语音的方法
通过在C#.net中使用JQuery Ajax调用此方法
但是它给出了以下错误

目前无法启动异步操作.
异步操作只能在异步
中启动 处理程序或模块,或者在页面生命周期中的某些事件期间.
如果在执行Page时发生此异常,请确保
页面被标记为<%@页面Async ="true"%>.此异常也可能表示尝试
调用异步无效"方法,ASP.NET请求处理通常不支持该方法.
相反,异步方法应该返回一个Task,而调用者应该等待它."







这是.aspx文件代码


<%@页面语言="C#" AutoEventWireup ="true" Async ="true" CodeBehind ="TextToSpeech.aspx.cs" Inherits ="KSPL.TextToSpeech.WebApp.TextToSpeech"%>

<!DOCTYPE html>

< html xmlns ="http://www.w3.org/1999/xhtml">
< head runat ="server">
< script type ="text/javascript">
$(function(){
$(''#btnSpeak'').click(function(){
$ .ajax({
类型:"POST",
contentType:"application/json; charset = utf-8",
网址:"TextToSpeech.aspx/SpeakText",
dataType:"json",
数据:"{''EnterText'':''" + $(''#txtEnterText'').val()+'',''Gender'':''" + $(''#ddlGender'' ).val()+''}",
成功:函数(数据){
alert(data.d);
},
错误:函数(结果){
警报(结果);
}
});
});
});
</script>
</head>

< body>
< asp:Button ID ="btnSpeak" runat ="server" Text ="Speak" ValidationGroup ="groupSpeek"/>  


</body>




这是.cs代码

[System.Web.Services.WebMethod]
公共静态字符串SpeakText(字符串EnterText,字符串Gender)
{

试试
{

if(EnterText!=")//如果文本区域不为空
{
SpeechSynthesizer reader =新的SpeechSynthesizer();//创建新对象
如果(性别==男")
{
reader.SelectVoiceByHints(VoiceGender.Male);//将语音男"设置为语音
}
否则,如果(性别==女性")
{
reader.SelectVoiceByHints(VoiceGender.Female);//将语音女"设置为语音
}

reader.Speak(EnterText); //SpeakAsync()方法将文本转换为语音
返回"abc";
}
返回"abc";
}
catch(ex ex例外)
{
扔前;
}

}

Please Help me.

I am write one method for converting Text To Speech in .cs file
This method call by using JQuery Ajax in C# .net
But it gives following Error

" An asynchronous operation cannot be started at this time.
Asynchronous operations may only be started within an asynchronous
handler or module or during certain events in the Page lifecycle.
If this exception occurred while executing a Page, ensure that the
Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to
call an "async void" method, which is generally unsupported within ASP.NET request processing.
Instead, the asynchronous method should return a Task, and the caller should await it."







This is .aspx File code


<%@ Page Language="C#" AutoEventWireup="true" Async="true" CodeBehind="TextToSpeech.aspx.cs" Inherits="KSPL.TextToSpeech.WebApp.TextToSpeech" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
$(function () {
$(''#btnSpeak'').click(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "TextToSpeech.aspx/SpeakText",
dataType: "json",
data: "{''EnterText'':''" + $(''#txtEnterText'').val() + "'',''Gender'':''" + $(''#ddlGender'').val() + "''}",
success: function (data) {
alert(data.d);
},
error: function (result) {
alert(result);
}
});
});
});
</script>
</head>

<body>
<asp:Button ID="btnSpeak" runat="server" Text="Speak" ValidationGroup="groupSpeek" />  


</body>




this is .cs code

[System.Web.Services.WebMethod]
public static string SpeakText(string EnterText, string Gender)
{

try
{

if (EnterText != "") //if text area is not empty
{
SpeechSynthesizer reader = new SpeechSynthesizer();//create new object
if (Gender == "Male")
{
reader.SelectVoiceByHints(VoiceGender.Male);// Set Voice Male to speech
}
else if (Gender == "Female")
{
reader.SelectVoiceByHints(VoiceGender.Female);// Set Voice Female to speech
}

reader.Speak(EnterText); // SpeakAsync() method convert text into speech
return "abc";
}
return "abc";
}
catch (Exception ex)
{
throw ex;
}

}

推荐答案

(function(){
(function () {


(``#btnSpeak'').click(function (){
(''#btnSpeak'').click(function () {


.ajax({
类型:"POST",
contentType:"application/json; charset = utf-8",
网址:"TextToSpeech.aspx/SpeakText",
dataType:"json",
数据:"{" EnterText:""+
.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "TextToSpeech.aspx/SpeakText",
dataType: "json",
data: "{''EnterText'':''" +


这篇关于如何使用ASP.NET AJAX和jQuery AJAX调用服务器端方法(包含文本到语音代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆