我创建了一个 WCF 服务并在客户端调用它。
<script lang="javascript" type="text/javascript">
var x;
function ttsFunction() {
var data = new Object();
data.text = $('#ddl').val();
var jsonString = JSON.stringify(data);
x = $.getJSON("http://localhost:8080/wscccService.svc/RunTts?jsoncallback=?", { text: jsonString });
}
$('#speak').val = x;
</script>
和服务
namespace wsccc1
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[JavascriptCallbackBehavior(UrlParameterName = "jsoncallback")]//"jsoncallback is custom"
public class wscccService : Iwservice
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public string RunTts(string length)
{
return Membership.GeneratePassword(Int32.Parse(length), 1);
}
}
}
接口代码:
namespace wsccc1
{
[ServiceContract]
public interface Iwservice
{
[OperationContract]
//[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
// BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "RunTts")]
string RunTts(string text);
}
}
例外情况如下:
我猜参数“length”有问题,它是json格式。感谢帮助。