无法通过GET AJAX web请求返回词典(字符串,字符串),可与POST [英] Can't return Dictionary(Of String, String) via GET ajax web request, works with POST
问题描述
我有以下web方法:
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=True, XmlSerializeString:=True)> _
Public Function GetDictionary() As Dictionary(Of String, String)
Dim d As New Dictionary(Of String, String)
d.Add("key1", "value1")
d.Add("key2", "value2")
Return d
End Function
我可以检索结果精(JSON)如果我使用HttpPost从我的AJAX调用,但只要我使用HTTPGET我得到以下异常:
I can retrieve the results fine (JSON) if I use HttpPost from my ajax call, but as soon as I use HttpGet I get the following exception:
System.NotSupportedException:类型System.Collections.Generic.Dictionary`2 [System.String,mscorlib中 ,版本= 2.0.0.0,文化=中性公钥= b77a5c561934e089],[System.String,mscorlib程序,版本 = 2.0.0.0,文化=中性公钥= b77a5c561934e089]]不支持,因为它实现IDictionary的
System.NotSupportedException: The type System.Collections.Generic.Dictionary`2[[System.String, mscorlib , Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version =2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] is not supported because it implements IDictionary
我想用HTTPGET这里,这样的结果可以被缓存。
I wanted to use HttpGet here so that the result can be cached.
我想尽变化要求这一点,但没有运气。有任何想法吗?与得到的是这可能吗?
I tried every variation of calling this, but no luck. Any ideas? Is this possible with GET?
推荐答案
另一种方法是改变返回类型为String,然后通过的 JavaScriptSerializer.Serialize 。这可能不是正是你与返回的字典中有意,但它会在JSON响应key = value对。
Another alternative is to change the Return type to a String and then convert the Dictionary to JSON via JavaScriptSerializer.Serialize. This may not be exactly what you were intending with returning a Dictionary but it will be key=value pairs in the JSON Response.
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=True, XmlSerializeString:=True)> _
Public Function GetDictionary() As String
Dim d As New Dictionary(Of String, String)
d.Add("key1", "value1")
d.Add("key2", "value2")
Return New JavaScriptSerializer().Serialize(d)
End Function
和生成的JSON:
{"key1":"value1","key2":"value2"}
这篇关于无法通过GET AJAX web请求返回词典(字符串,字符串),可与POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!