title: Deserializing JSON in API fuction works from VS/Browser, not when called from jQuery
body: <p>I have the following class in a VB project, it's an ASMX service (bit old fashioned, I know):</p>
  
  <pre><code>Public Class cName
      Public Property nameOrAlias As String
      Public Property nameType As String
      Public Property isVerified As String
      Public Property nameID As String
      Public Property icn As String
  End Class
  </code></pre>
  
  <p>And I use it in the following function:</p>
  
  <pre><code>&lt;WebMethod()&gt;
      Public Function SaveCaseNames(jsn As String) As String
  
          Try
              Dim json As String = jsn
  
              Dim d As New DataTable
              Dim c As New cShared
  
              Dim s As New System.Web.Script.Serialization.JavaScriptSerializer
              Dim sql As String = ""
              Dim icn As String = ""
              Dim sArgs As String = ""
  
              Dim o As Object = s.Deserialize(Of Object)(json)
  
  
              Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
              Dim lstNames = js.Deserialize(Of List(Of cName))(json)
  
              'First we look for new names, those without a nameID
              For Each cx As cName In lstNames
  
                  icn = cx.icn.ToString
                  If cx.nameID.ToString = "--" Then
  
                      sArgs &amp;= "'" &amp; icn &amp; "', "
                      sArgs &amp;= "'" &amp; cx.nameOrAlias.ToString &amp; "', "
                      sArgs &amp;= "'" &amp; cx.nameType.ToString &amp; "', "
  
                      Select Case cx.isVerified.ToString
                          Case "YES"
                              sArgs &amp;= "1"
                          Case "NO"
                              sArgs &amp;= "0"
                      End Select
  
                      sql = "sp_insertCaseNames " &amp; sArgs
                      d = c.GetDataTable(c.SetConnection(), sql)
                      d.TableName = "insertNames"
  
                  End If
  
  
  
              Next
  
              'if it has a D the entry should be deleted.
              For Each cx As cName In lstNames
                  If cx.nameID.ToString.StartsWith("D") Then
                      Dim nameID As String = Replace(cx.nameID.ToString, "D", "")
                      sql = "UPDATE casenames Set st_id = 4 WHERE id = " &amp; nameID
                      d = c.GetDataTable(c.SetConnection(), sql)
                      d.TableName = "deleteNames"
                  End If
              Next
  
              Return "HELLO WORLD!"
  
          Catch ex As Exception
              Debug.Print(ex.Message.ToString)
          End Try
  
      End Function
  </code></pre>
  
  <p>Using tables is a bit funny of course since neither of the two data calls returns anything but I am not done with writing the helper functions yet and at any rate isn't what this question is about.</p>
  
  <p>If I run the project in VS 2015 it runs fine. I pass the following JSON to the function above and all works. </p>
  
  <pre><code>[{"nameOrAlias":"TEST 4","nameType":"MIDDLE","isVerified":"YES","nameID":"16","icn":"N2016000020"},{"nameOrAlias":"TEST1","nameType":"AKA","isVerified":"YES","nameID":"D15","icn":"N2016000020"},{"nameOrAlias":"TESTXXX101","nameType":"AKA","isVerified":"YES","nameID":"17","icn":"N2016000020"},{"nameOrAlias":"HELLO JOE","nameType":"AKA","isVerified":"YES","nameID":"--","icn":"N2016000020"}]
  </code></pre>
  
  <p>The record with "TEST1" is deleted and "HELLO JOE" is added. </p>
  
  <p>When I call that exact same function with the exact same JSON array from my web page using jQuery, like so:</p>
  
  <pre><code>//Save the case names (aliases)
  var jsonString = sessionStorage.getItem('caseNames');
  
  
  var str = JSON.stringify(jsonString);
  if (debug == 'true') {
      alert(str);
  }
  
  $.ajax({
      type: "POST",
      url: "http://myserver.net/dps/svc/DeadSimple.asmx/SaveCaseNames",
      data: str,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (response) {
          alert('YAY!!!!!!!!!');
      },
      failure: function (response) {
          alert('Fail! ' + response.d);
      }
  });
  </code></pre>
  
  <p>I get an error message:</p>
  
  <blockquote>
    <p>{"Message":"Cannot convert object of type \u0027System.String\u0027 to
    type
    \u0027System.Collections.Generic.IDictionary`2[System.String,System.Object]\u0027","StackTrace":"
    at
    System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object
    o, Type type, JavaScriptSerializer serializer, Boolean throwOnError,
    Object\u0026 convertedObject)\r\n   at
    System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object
    o, Type type, JavaScriptSerializer serializer, Boolean throwOnError,
    Object\u0026 convertedObject)\r\n   at
    System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String
    input)\r\n   at
    System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext
    context, WebServiceMethodData
    methodData)","ExceptionType":"System.InvalidOperationException"}</p>
  </blockquote>
  
  <p>I have tried this with and without <code>JSON.stringify</code>, no success. I also wrapped the array as in: <code>var str = JSON.stringify('{data:' + jsonString + '}');</code> but that just made it harder to parse because now the objects I am after were a level deeper and it didn't solve the issue anyway.</p>
  
  <p>What is causing this? If I F5 the web service in VS and call it directly from the browser, it works. HELP! the Target framework for the class lib is 3.5</p>