Question

I create a WCF service and call it in client side.

<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>

And the service

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);
    }
 }
}

The interface code:

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);
  }
 }

The exception is as below: error

I guess that something wrong on the parameter "length", it is a json format. Thanks for help.

Was it helpful?

Solution

You need to use a JSON deserializer to read the length variable. Right now you are trying to Int32.Parse("{\"text\":\"8\"}"), which doesn't make sense.

Example (this is for Newtonsoft JSON):

public string RunTts(string lengthObject) {
    JToken token = JObject.Parse(lengthObject);
    int length = (int)token.SelectToken("text");
    return Membership.GeneratePassword(length, 1);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top