I have an applicatiuon MVC wirtten in c#.
I want to make ajax post request to my controller method:
locationId = "2087";
jQuery.ajax({
url: rootUrl + "SpaEmployeeAdmin/GetCurrentLocationDateTime",
contentType: "application/json; charset=utf-8",
data: "locationId=" + locationId,
dataType: "json",
type: "POST",
async: false,
success: function (data) {
//data.someObject
}
});
My signature of a method looks like:
[HttpPost]
public ActionResult GetCurrentLocationDateTime(int? locationId = null)
{}
but however, I’m receiving an error exception of this ajax call:
"System.ArgumentException: Invalid JSON primitive: locationId.
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
at System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext)
at System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext)
at Castle.Proxies.Invocations.ValueProviderFactory_GetValueProvider.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Glimpse.Core.Extensibility.CastleInvocationToAlternateMethodContextAdapter.Proceed()
at Glimpse.Core.Extensions.AlternateMethodContextExtensions.TryProceedWithTimer(IAlternateMethodContext context, TimerResult& timerResult)
at Glimpse.Core.Extensibility.AlternateMethod.NewImplementation(IAlternateMethodContext context)
at Glimpse.Core.Extensibility.AlternateTypeToCastleInterceptorAdapter.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.ValueProviderFactoryProxy.GetValueProvider(ControllerContext controllerContext)
at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext)
at System.Web.Mvc.ControllerBase.get_ValueProvider()
at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState)"
how to write properly thisparameter? without parameter and removing the input argument from my method, ajax call works.
2
Answers
It’s having trouble parsing your data so you will need to serialize it as json before posting. It would help to use a LocationRequest model as well.
Try this:
The
data
parameter for theajax
call should be formatted as a JSON object like the following:By convention, the server side C# expects the body of the request to be JSON, with the method parameter names defined as properties on the JSON object.