【问题标题】:Invalid web service call, missing value for parameter: \u0027sentQuery\u0027 & Invalid JSON primitive无效的 Web 服务调用,缺少参数值:\u0027sentQuery\u0027 & 无效的 JSON 原语
【发布时间】:2015-10-07 17:33:56
【问题描述】:

不知道为什么我得到这个但我得到这个错误:

Invalid web service call, missing value for parameter: \u0027sentQuery\u0027

尝试对我的 ASPX Web 服务执行 jQuery AJAX 时。

我的 AJAX 是这样的:

$.ajax({
    type: 'GET',
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    url: "http://localhost:7665/Service1.asmx/theQ",
    data: "{\"sentQuery\":" + "\"SELECT OPRID FROM vwPS_BC_JOB_PERS_DAT\"" + "}",
    success: function (data) {
        console.log(data);
    },
    error: function (a) {
        alert('ERROR: ' + a.responseText);
    }
});

还有我的 VB 网络服务代码:

<WebMethod(CacheDuration:=60)> _
<ScriptMethod(UseHttpGet:=True, ResponseFormat:=ResponseFormat.Json, XmlSerializeString:=False)> _
Public Sub theQ(ByVal sentQuery As String)
    Dim results As Object = fetchSQLQ("query", sentQuery)

    Try
        Dim ser As New System.Web.Script.Serialization.JavaScriptSerializer()
        Dim strResponse As String = ser.Serialize(results)

        Context.Response.Clear()
        Context.Response.ContentType = "application/json"
        Context.Response.AddHeader("content-length", strResponse.Length.ToString())
        Context.Response.Write(strResponse)
        HttpContext.Current.ApplicationInstance.CompleteRequest()
    Catch ex As Exception
        Context.Response.Clear()
        Context.Response.ContentType = "application/json"
        Context.Response.AddHeader("content-length", ex.Message.Length.ToString())
        Context.Response.Write(String.Format("[ERROR: {0}]", ex.Message))
        HttpContext.Current.ApplicationInstance.CompleteRequest()
    End Try
End Sub

更新

我猜以下是正确的:

data: { sentQuery: "SELECT OPRID FROM vwPS_BC_JOB_PERS_DAT" },

但是,它确实会产生另一个错误:

Invalid JSON primitive: SELECT.

嗯????

【问题讨论】:

  • 永远不要手动创建 json,这很容易出错,而且你已经犯了错误
  • 我认为asp.net不会自动解析url中的json字符串,你为什么不使用普通的?key=value查询字符串

标签: jquery asp.net ajax vb.net web-services


【解决方案1】:

手动创建 json 容易出错,而且由于难以阅读,也难以调试。

让javascript中的JSON.stringify等序列化方法为你代劳

试试

data: JSON.stringify({sentQuery:"SELECT OPRID FROM vwPS_BC_JOB_PERS_DAT"}),

【讨论】:

  • 虽然我同意不手动制作 JSON,但我没有看到任何错误。
  • @Musa 我认为你是对的,我确实误解了......难以阅读的手动 json 也是我的观点的一部分
  • 我刚刚用正确的方式更新了我的 OP 以发送 JSON,但同时也收到了新的错误消息。
  • 但现在您不发送 json ...我只需将其作为默认表单编码发送并作为帖子接收。我删除内容类型然后它与处理常规表单没有什么不同
  • @charlietfl 你能给我看一个你在说什么的例子吗?
【解决方案2】:

看完Chris Brandsma的帖子终于明白了。

AJAX 代码:

$.ajax({
   type: 'POST',
   contentType: 'application/json; charset=utf-8',
   dataType: 'json',
   url: 'http://localhost:7665/Service1.asmx/theQ',
   data: JSON.stringify({ qString: ["SELECT OPRID FROM vwPS_BC_JOB_PERS_DAT"] }),
   async: true,
   cache: false,
   success: function (data) {
       console.log(data);
   },
   error: function (a) {
       alert('ERROR: ' + a.responseText);
   }
});

VB.net 代码:

<WebMethod(CacheDuration:=60)> _
<ScriptMethod(UseHttpGet:=False, ResponseFormat:=ResponseFormat.Json, XmlSerializeString:=False)> _
Public Sub theQ(qString As List(Of String))
    Dim results As Object = fetchSQLQ("query", qString(0))

    Try
        Dim ser As New System.Web.Script.Serialization.JavaScriptSerializer()
        Dim strResponse As String = ser.Serialize(results)

        Context.Response.Clear()
        Context.Response.ContentType = "application/json"
        Context.Response.AddHeader("content-length", strResponse.Length.ToString())
        Context.Response.Write(strResponse)
        HttpContext.Current.ApplicationInstance.CompleteRequest()
    Catch ex As Exception
        Context.Response.Clear()
        Context.Response.ContentType = "application/json"
        Context.Response.AddHeader("content-length", ex.Message.Length.ToString())
        Context.Response.Write(String.Format("[ERROR: {0}]", ex.Message))
        HttpContext.Current.ApplicationInstance.CompleteRequest()
    End Try
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    相关资源
    最近更新 更多