无效的Web服务调用,缺少参数值:\ u0027sentQuery \ u0027&无效的JSON原语

时间:2015-10-07 17:33:57

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

不确定我为什么会这样,但是我收到了这个错误:

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

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

我的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.

咦????

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

在阅读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