0

Not sure why I am getting this but I get this error:

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

When trying to execute jQuery AJAX to my ASPX web service.

My AJAX is this:

$.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);
    }
});

And my VB web service code:

<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

UPDATE

I am guessing that the following is correct:

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

However, it does produce another error:

Invalid JSON primitive: SELECT.

Huh????

StealthRT
  • 10,108
  • 40
  • 183
  • 342
  • never create json manually, it is error prone and you have already made errors doing it – charlietfl Oct 07 '15 at 17:38
  • I don't think asp.net will parse a json string in a url automatically, why dont you use a normal ?key=value query string – Musa Oct 07 '15 at 17:57

2 Answers2

0

Creating the json manually is error prone and as a result of being hard to read is also hard to debug.

Let serialization methods such as JSON.stringify in javascript do it for you

try

data: JSON.stringify({sentQuery:"SELECT OPRID FROM vwPS_BC_JOB_PERS_DAT"}),
StealthRT
  • 10,108
  • 40
  • 183
  • 342
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • 1
    While I agree with not making JSON manually, I don't see any error in it. – Musa Oct 07 '15 at 17:55
  • @Musa i think you're right and i did misinterpret... hard to read manual json is part of my point also – charlietfl Oct 07 '15 at 17:55
  • I just updated my OP with the correct way to send the JSON but also get a new error message in doing so. – StealthRT Oct 07 '15 at 18:25
  • 1
    but now you aren't sending json ...I would simply send as default form encoding and receive as post. I remove the content type then it is no diferent than processing a regular form – charlietfl Oct 07 '15 at 18:27
  • @charlietfl can you show me an example of what you are talking about? – StealthRT Oct 07 '15 at 18:37
  • I don't know .NET at all ... but default ajax sends data as form encoded key/value pairs the same way a `
    ` does with `name` on input and it's value
    – charlietfl Oct 07 '15 at 18:40
0

Finally got this after reading Chris Brandsma post.

AJAX code:

$.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 code:

<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
Community
  • 1
  • 1
StealthRT
  • 10,108
  • 40
  • 183
  • 342