【问题标题】:Value of type 'String' cannot be converted to '1-dimensional array of String' webservice call“字符串”类型的值无法转换为“字符串的一维数组”Web 服务调用
【发布时间】:2015-10-23 17:16:34
【问题描述】:

嘿,我得到的错误是:

“字符串”类型的值无法转换为“字符串的一维数组”。

在我的代码的这一行:

Dim blah As String = webService.theQ(qString:="SELECT blah FROM table WHERE blah = 'hello'")

我的web服务sqlQ功能代码是:

<WebMethod(CacheDuration:=60)> _
<ScriptMethod(UseHttpGet:=False, ResponseFormat:=ResponseFormat.Json, XmlSerializeString:=False)> _
Public Function theQ(ByVal qString As List(Of String)) As String
    Dim results As Object = fetchSQLQ("query", qString(0))
    ...etc etc
    Dim ser As New System.Web.Script.Serialization.JavaScriptSerializer()
    Dim strResponse As String = ser.Serialize(results)

    return strResponse
End Function

我不确定如何按照它的要求格式化它?

这就是我使用 AJAX 调用它的方式(它工作正常):

var sqlCC = "SELECT blah FROM table WHERE blah = 'hello'";

$.ajax({
    type : "POST",
    crossDomain : true,
    dataType : 'json',
    cache : false,
    contentType : "application/json",
    url : "/Service1.asmx/theQ",
    data : JSON.stringify({
        qString : [sqlCC]
    }),
    success : function (data2) {
        var obj2 = jQuery.parseJSON(data2);
    },
    error : function (xhr, status, error) {
        console.log(xhr.responseText);
    }
});

任何帮助都会很棒!

【问题讨论】:

  • qString 的类型为 List(Of String) 但您只传递一个字符串; theQ 是一个Sub,它不产生任何返回值,但你将它用作Function
  • @Sehnsucht 检查我的 OP 以获取更多 Web 服务代码,以更好地解释我返回的内容。
  • @StealthRT 请看我的回答 - 它解释了你做错了什么

标签: arrays vb.net web-services arraylist


【解决方案1】:

您的问题是您传递给您的网络服务的参数。你通过了

webService.theQ(qString:="SELECT blah FROM table WHERE blah = 'hello'")

您将字符串分配给qString 的位置。但在你的声明中

Public Sub theQ(ByVal qString As List(Of String))

qString 被声明为字符串列表。

而且,您的网络方法是 SUB,它没有任何返回值,因此

Dim blah As List(Of String) = webService.theQ(. . . .

甚至不合法

【讨论】:

    【解决方案2】:

    错误几乎说明了一切。 webservice 调用返回一个字符串,而不是字符串列表。如果您查看您的脚本代码,它会将 AJAX 调用的结果解析为 JSON。虽然我无法从您的其余代码中确定,但很可能 Web 服务调用也返回了 JSON 字符串。

    简单的确认方式:

    Dim blah as String = webService.theQ(...)
    ' Print out the value of blah or inspect in debugger... 
    

    如果字符串是 JSON,那么您必须对其进行解析以获取您要查找的列表。

    【讨论】:

    • Using Dim blah As String 仍然有同样的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    相关资源
    最近更新 更多