I am getting this error when trying to parse through 2 Variables both of them are int values.
Here is the exact error message I get:
error - {"Message":"Invalid JSON primitive: warehouseID.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
Here is My Java Script:
var viewModel;
var locations = ko.observableArray([]);
function bindModel(data) {
viewModel = ko.mapping.fromJS(data, {}, locations);
console.log(viewModel);
ko.applyBindings(viewModel);
}
//Onload ObtainJSON
$("#btnShowProducts").click(JSON(1,2));
function JSON(varWarehouse, varProduct) {
$.ajax({
url: "WebForm1.aspx/StockPlacementOptions",
// Current Page, Method
data: { warehouseID: varWarehouse, Productskuid: varProduct },
// parameter map as JSON
type: "POST",
// data has to be POSTed
contentType: "application/json; charset=utf-8",
// posting JSON content
dataType: "JSON",
// type of data is JSON (must be upper case!)
timeout: 10000,
// AJAX timeout
success: function (result) {
bindModel(result);
},
error: function (xhr, status) {
console.log(status + " - " + xhr.responseText);
}
});
}
And Finally the WebMethod I am Accessing:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static List<StockReturnMethod> StockPlacementOptions(object warehouseID, object Productskuid)
{
scmEntitiesPrimaryCon entities = new scmEntitiesPrimaryCon();
int Warehouse = new JavaScriptSerializer().ConvertToType<int>(warehouseID);
int Product = new JavaScriptSerializer().ConvertToType<int>(Productskuid);
var binOptions = (from avail in entities.ProductAvailibleBins(Warehouse, Product)
select new StockReturnMethod() { LotID = (int)avail.LotID, LotName = avail.LotName, AreaID = (int)avail.AreaID, AreaName = avail.AreaName, BinID = (int)avail.BinID, BinName = avail.BinName }).ToList();
return binOptions;
}
As you can see I am using the Serializer to Convert the Values from Object to Int. And still no luck.
There is My JSON that I want to Retrieve from the server, This Is the format I get back. The problem comes in when I want to parse through the variables in the webmethod: warehouseID and Productskuid. That is when I get the Primitive Property error.
LotID is part of my class that I defined to return the JSON:
[Serializable]
public class StockReturnMethod
{
public int LotID { get; set; }
public string LotName { get; set; }
public int AreaID { get; set; }
public string AreaName { get; set; }
public int BinID { get; set; }
public string BinName { get; set; }
}
Jacques444
Member
41 Points
54 Posts
Invalid JSON Primitive error when Trying to Parse int variable to webmethod
Dec 04, 2012 11:00 AM|LINK
Hey Guys,
I am getting this error when trying to parse through 2 Variables both of them are int values.
Here is the exact error message I get:
error - {"Message":"Invalid JSON primitive: warehouseID.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}Here is My Java Script:
var viewModel; var locations = ko.observableArray([]); function bindModel(data) { viewModel = ko.mapping.fromJS(data, {}, locations); console.log(viewModel); ko.applyBindings(viewModel); } //Onload ObtainJSON $("#btnShowProducts").click(JSON(1,2)); function JSON(varWarehouse, varProduct) { $.ajax({ url: "WebForm1.aspx/StockPlacementOptions", // Current Page, Method data: { warehouseID: varWarehouse, Productskuid: varProduct }, // parameter map as JSON type: "POST", // data has to be POSTed contentType: "application/json; charset=utf-8", // posting JSON content dataType: "JSON", // type of data is JSON (must be upper case!) timeout: 10000, // AJAX timeout success: function (result) { bindModel(result); }, error: function (xhr, status) { console.log(status + " - " + xhr.responseText); } }); }And Finally the WebMethod I am Accessing:
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static List<StockReturnMethod> StockPlacementOptions(object warehouseID, object Productskuid) { scmEntitiesPrimaryCon entities = new scmEntitiesPrimaryCon(); int Warehouse = new JavaScriptSerializer().ConvertToType<int>(warehouseID); int Product = new JavaScriptSerializer().ConvertToType<int>(Productskuid); var binOptions = (from avail in entities.ProductAvailibleBins(Warehouse, Product) select new StockReturnMethod() { LotID = (int)avail.LotID, LotName = avail.LotName, AreaID = (int)avail.AreaID, AreaName = avail.AreaName, BinID = (int)avail.BinID, BinName = avail.BinName }).ToList(); return binOptions; }As you can see I am using the Serializer to Convert the Values from Object to Int. And still no luck.
Any Advice?
thaicarrot
Contributor
5120 Points
1459 Posts
Re: Invalid JSON Primitive error when Trying to Parse int variable to webmethod
Dec 04, 2012 03:29 PM|LINK
Please use the fiddler to check the income json data then post the whole JSON syntax here again.
Are you sure the input JSON data is correct at all?
Weera
Jacques444
Member
41 Points
54 Posts
Re: Invalid JSON Primitive error when Trying to Parse int variable to webmethod
Dec 04, 2012 08:55 PM|LINK
Hi Weera,
{ "d": [ { "__type": "Warehouse.Tracntrace.Members_Only.StockMovement.StockReturnMethod", "LotID": 3, "LotName": "TestLot3", "AreaID": 11, "AreaName": "TestArea2L3", "BinID": 20, "BinName": "Area10Bin1" }, { "__type": "Warehouse.Tracntrace.Members_Only.StockMovement.StockReturnMethod", "LotID": 4, "LotName": "TestLot4", "AreaID": 15, "AreaName": "TestArea2L4", "BinID": 24, "BinName": "Area14Bin1" }, { "__type": "Warehouse.Tracntrace.Members_Only.StockMovement.StockReturnMethod", "LotID": 2, "LotName": "TestLot2", "AreaID": 8, "AreaName": "TestArea3L2", "BinID": 18, "BinName": "Area8Bin2" } ] }There is My JSON that I want to Retrieve from the server, This Is the format I get back. The problem comes in when I want to parse through the variables in the webmethod: warehouseID and Productskuid. That is when I get the Primitive Property error.
Also post Response to Server is like this:
Jacques444
Member
41 Points
54 Posts
Re: Invalid JSON Primitive error when Trying to Parse int variable to webmethod
Dec 04, 2012 09:50 PM|LINK
Made some Headway, by forcing the data: property in the ajax post to string it parses correctly.
data: '{warehouseID: 1, Productskuid: 2}',But as soon as I declare 2 var's
And add them to that data property like this:
data: '{warehouseID: varWarehouseID, Productskuid: varProductSkuID}',I am back to square one where it gives the same error as I started with....
thaicarrot
Contributor
5120 Points
1459 Posts
Re: Invalid JSON Primitive error when Trying to Parse int variable to webmethod
Dec 04, 2012 10:20 PM|LINK
Why not use JSON.net libary?
Weera
Jacques444
Member
41 Points
54 Posts
Re: Invalid JSON Primitive error when Trying to Parse int variable to webmethod
Dec 05, 2012 03:51 AM|LINK
Hi Weera,
What would be the benifit of this?
Also how would I use it in this piticular instance?
Regards
Jacques
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Invalid JSON Primitive error when Trying to Parse int variable to webmethod
Dec 05, 2012 04:02 AM|LINK
What's the type of LotID? Is that of object?
Jacques444
Member
41 Points
54 Posts
Re: Invalid JSON Primitive error when Trying to Parse int variable to webmethod
Dec 05, 2012 04:04 AM|LINK
LotID is part of my class that I defined to return the JSON:
[Serializable] public class StockReturnMethod { public int LotID { get; set; } public string LotName { get; set; } public int AreaID { get; set; } public string AreaName { get; set; } public int BinID { get; set; } public string BinName { get; set; } }Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Invalid JSON Primitive error when Trying to Parse int variable to webmethod
Dec 05, 2012 04:11 AM|LINK
Well……since LotID is type of int, no need to convert to int again;)
And maybe you can try to analyze it with .ToString() method. I mean:
var binOptions = (from avail in entities.ProductAvailibleBins(Warehouse, Product) select new StockReturnMethod() { LotID = avail.LotID.ToString(), LotName = avail.LotName, AreaID,……}
raju dasa
Star
14320 Points
2440 Posts
Re: Invalid JSON Primitive error when Trying to Parse int variable to webmethod
Dec 05, 2012 08:28 AM|LINK
Hi,
Try modifying ur ajax data like this:
data: { "warehouseID": parseInt(varWarehouse), "Productskuid": varProduct }, dataType: "json",if not works, try from firefox browser.
rajudasa.blogspot.com || blog@opera