This is a migrated thread and some comments may be shown as answers.

ERROR: calling ASP.NET JSON webservice with parameters

5 Answers 1599 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Roland Koops
Top achievements
Rank 1
Roland Koops asked on 26 Jan 2012, 09:58 AM
I have create a test page with a grid. I consume a simple webservice (mobileservice.asmx). I have configured my dataasource and webservice to use JSON. However as soon as i want to sent parameters the webservice call fails with errors like:

{"Message":"Invalid JSON primitive: vn.","StackTrace":" bij System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n bij System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n bij System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n bij System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n bij System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n bij System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n bij System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n bij System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

it seems that webservice parameters are not sent as JSON.

mobileservice.asmx 
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Item[] GetRecords2(string vn)
{
    IDataView view = DatabaseSession.Current.GetOrCreateView(vn);
    List<Item> items = new List<Item>(view.GetRecords(0, 40).Cast<IItemRecord>().Select(i => new Item() { Code = i.Code, Description = i.Description }));
    //count = view.RecordCount;
  
    return items.ToArray();
}
  
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Item[] GetRecords3()
{
    IDataView view = DatabaseSession.Current.GetOrCreateView("prd.item");
    List<Item> items = new List<Item>(view.GetRecords(0, 40).Cast<IItemRecord>().Select(i => new Item() { Code = i.Code, Description = i.Description }));
    //count = view.RecordCount;
  
    return items.ToArray();
}
  
public class Item
{
    public string Code { get; set; }
    public string Description { get; set; }
}

successful page, no parameters:
$(document).ready(function () {
    window.kendoMobileApplication = new kendo.mobile.Application(document.body);
    $("#grid").kendoGrid({
        dataSource: {
            transport: {
                read: {
                    type: "POST",
                    url: "/webservices/mobile.asmx/GetRecords3",
                    contentType: "application/json; charset=utf-8",
                    data: {},
                    dataType: "json"
                }
            },
        schema: {
            data: "d",
            model: {
                fields: {
                    __type : { type: "string" },
                    Code: { type: "string" },
                    Description: { type: "string" },
                }
            }
        },
        error: onError
        },
        dataBound: ondataBound,
        columns: [
            { title: "Code", field: "Code" },
            { title: "Description", field: "Description"}],
        height: 300
    });

if i change to the configuration below i get the error:
{"Message":"Invalid JSON primitive: vn.","StackTrace":" bij System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n bij System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n bij System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n bij System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n bij System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n bij System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n bij System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n bij System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

$(document).ready(function () {
    window.kendoMobileApplication = new kendo.mobile.Application(document.body);
    $("#grid").kendoGrid({
        dataSource: {
            transport: {
                read: {
                    type: "POST",
                    url: "/webservices/mobile.asmx/GetRecords2",
                    contentType: "application/json; charset=utf-8",
                    data: { vn : "prd.item"},
                    dataType: "json"
                }
            },
        schema: {
            data: "d",
            model: {
                fields: {
                    __type : { type: "string" },
                    Code: { type: "string" },
                    Description: { type: "string" },
                }
            }
        },
        pageSize: 10,
        serverPaging: true,
        error: onError
        },
        dataBound: ondataBound,
        columns: [
            { title: "Code", field: "Code" },
            { title: "Description", field: "Description"}],
        height: 300
    });

closer expection reveals that the parameters are not send as JSON:

  1. Request URL:
    http://localhost:51200/webservices/mobile.asmx/GetRecords2
  2. Request Method:
    POST
  3. Status Code:
    500 Internal Server Error
  4. Request Headersview source
    1. Accept:
      application/json, text/javascript, */*; q=0.01
    2. Accept-Charset:
      ISO-8859-1,utf-8;q=0.7,*;q=0.3
    3. Accept-Encoding:
      gzip,deflate,sdch
    4. Accept-Language:
      nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
    5. Connection:
      keep-alive
    6. Content-Length:
      11
    7. Content-Type:
      application/json; charset=UTF-8
    8. Cookie:
      ASP.NET_SessionId=izwu2vgysxrijnb4iszwndmk
    9. Host:
      localhost:51200
    10. Origin:
      http://localhost:51200
    11. Referer:
      http://localhost:51200/pages/mobile.htm
    12. User-Agent:
      Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7
    13. X-Requested-With:
      XMLHttpRequest
  5. Request Payload
    1. vn=prd.item
  6. Response Headersview source
    1. Cache-Control:
      private
    2. Connection:
      Close
    3. Content-Length:
      1068
    4. Content-Type:
      application/json; charset=utf-8
    5. Date:
      Thu, 26 Jan 2012 08:56:30 GMT
    6. Server:
      ASP.NET Development Server/10.0.0.0
    7. X-AspNet-Version:
      4.0.30319
    8. jsonerror:
      true

5 Answers, 1 is accepted

Sort by
0
Randall
Top achievements
Rank 1
answered on 27 Jan 2012, 03:41 AM
I have just been through this today, and this article solved my problem:
http://encosia.com/asmx-scriptservice-mistake-invalid-json-primitive/ 
Try this and see if it works:
data: '{ vn : "prd.item"} ',
0
Roland Koops
Top achievements
Rank 1
answered on 27 Jan 2012, 08:56 AM

Thx for the reply. That is an excellent article. I already read it. I have tried the solution mentioned in the article, but it didn't solve my problem. Furthermore if i turn on serverside paging, the datasource itself will add parameters.

I think i face the following problem:
In order to

  • have an ASP.NET webservice respond JSON, i have to set request contentType to application/json. I agree that this is a weird requirement imposed by MicroSoft.
  • The Kendo datasource however does not send its parameters in JSON format, when the contentType is set to JSON. This results in an exception from the webserver

.

0
Accepted
Atanas Korchev
Telerik team
answered on 27 Jan 2012, 09:37 AM
Hello,

 The Kendo DataSource uses jQuery.ajax to make ajax request. The latter does not encode the parameters as JSON by default. You need to do this via code. The Kendo DataSource allows this via the parameterMap method:

parameterMap: function(options) {
     return kendo.stringify(options); // kendo.stringify serializes to JSON string
}

Here is a fully working demo showing ASMX binding.

Regards,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Roland Koops
Top achievements
Rank 1
answered on 27 Jan 2012, 11:16 AM
Thanks Atanas, that is the answer i was looking for.
0
Mangesh
Top achievements
Rank 1
answered on 27 Jan 2012, 10:07 PM
Hi Atanas,

Can you please add filtering option to grid in https://github.com/telerik/kendo-examples-asp-net/tree/master/grid-web-service-crud this option? As I find difficulties in filtering json data which comes from asp.net web service

Thanks,
Mangesh
Tags
Data Source
Asked by
Roland Koops
Top achievements
Rank 1
Answers by
Randall
Top achievements
Rank 1
Roland Koops
Top achievements
Rank 1
Atanas Korchev
Telerik team
Mangesh
Top achievements
Rank 1
Share this question
or