locked
No parameterless constructor defined for type of 'System.String RRS feed

  • Question

  • User-1585644282 posted

    I want to call my web service (ASMX) from AJAX Jquery.

    when I tried my web service, it success retrieve the token, but when I try to call from AJAX, it gives me error "No parameterless constructor defined for type of 'System.String".

    here is my code for AJAX

    function retrieveToken()
    {
        var BodyToken = JSON.stringify({
            "userName": "crm"
        });
        var param1= {
            Token : BodyToken
        }
    
        var param2 = JSON.stringify({
            param1
        });
        $.ajax({
            url: "http://10.23.64.43:8035/iFrameIntegration.asmx/getToken",
            data: {Token : BodyToken},
            contentType: "application/json; charset=utf-8",
            type: 'GET',
            success: function (data) {
                alert(data);
            },
            error: function (data) {
                alert("Error");
            }
        });
    }

    and here is my web method (ASMX)

    [WebMethod]
            [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
            public void getToken(string Token)
            {
                var request = (HttpWebRequest)WebRequest.Create("http://10.23.64.37:8080/ACCMWS/member/SSOInit");
                var DataObject = Token;
                var data = Encoding.ASCII.GetBytes(DataObject);
                request.Method = "POST";
                request.ContentType = "application/json";
                //request.Headers["Content-Type"] = "application/json";
                request.Headers["userId"] = "Svc_CRM";
                request.Headers["loginType"] = "Internal";
                request.Headers["token"] = "54a93982adf51adfb81885ddbbb1874e271605ce";
    
                try
                {
                    request.ContentLength = data.Length;
                    Stream dataStream = request.GetRequestStream();
                    dataStream.Write(data, 0, data.Length);
                    dataStream.Close();
                    WebResponse response = request.GetResponse();
                    Stream resultStream = response.GetResponseStream();
    
                    StreamReader reader = new StreamReader(resultStream);
                    string readerResponse = reader.ReadToEnd();
                    var result = JsonConvert.DeserializeObject<RetrieveSSOTokenResult>(readerResponse);
                    //return result.data.ssotokenList[0].ssoToken;
                    PrintValue(result.data.ssotokenList[0].ssoToken);
                }
                catch(Exception ex)
                {
                    ex.Message.ToString();
                }
                
            }
    
            private void PrintValue(object obj)
            {
                Context.Response.Write(obj.ToString());
            }

    here is also my class for deserialize

    public class RetrieveSSOTokenResult
        {
            public string status { get; set; }
            public string message { get; set; }
            public Data data { get; set; }
        }
    
        public class Data
        {
            public Ssotokenlist[] ssotokenList { get; set; }
        }
    
        public class Ssotokenlist
        {
            public string loginType { get; set; }
            public string userName { get; set; }
            public string userId { get; set; }
            public string ssoToken { get; set; }
            public long expiryTime { get; set; }
        }

    the sample of the result is like this

    {
      "status": "success",
      "message": "",
      "data": {
        "ssotokenList": [
          {
            "loginType": "Internal",
            "userName": "CRM",
            "userId": "E512E6D2-1584-4597-BBD6-01C724496107",
            "ssoToken": "3d5930ea79092f89269bc4eae05ecbd435627a24",
            "expiryTime": 1542893295895
          }
        ]
      }
    }                       
    

    please advise

    Friday, January 18, 2019 9:55 AM

All replies

  • User197322208 posted

    1.

    it gives me error "No parameterless constructor defined for type of 'System.String".

    Which line  you have this error?

    2. I would call 

    $.ajax({
            url: "http://10.23.64.43:8035/iFrameIntegration.asmx/getToken",
            data: BodyToken,
    Friday, January 18, 2019 11:50 AM