将参数传递给 ajax 发布请求会返回错误
Posted
技术标签:
【中文标题】将参数传递给 ajax 发布请求会返回错误【英文标题】:passing argument to an ajax post request returns an error 【发布时间】:2021-10-16 19:42:13 【问题描述】:我有一个用 c# 编写的应用程序 MVC。 我想向我的控制器方法发出 ajax 发布请求:
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
);
我的方法签名如下:
[HttpPost]
public ActionResult GetCurrentLocationDateTime(int? locationId = null)
但是,我收到此 ajax 调用的错误异常:
"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)"
如何正确编写这个参数?没有参数并从我的方法中删除输入参数,ajax 调用有效。
【问题讨论】:
【参考方案1】:它无法解析您的数据,因此您需要在发布之前将其序列化为 json。使用 LocationRequest 模型也会有所帮助。
试试这个:
locationId = 2087;
jQuery.ajax(
url: rootUrl + "SpaEmployeeAdmin/GetCurrentLocationDateTime",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(locationId), //serialize json
dataType: "json",
type: "POST",
async: false,
success: function (data)
//data.someObject
);
public class LocationRequest
public int LocationId get; set;
[HttpPost]
public ActionResult GetCurrentLocationDateTime(LocationRequest request)
//request.LocationId
【讨论】:
谢谢,有没有可能没有LocationRequest,只有一个参数int? locationId
?
在这种情况下,您可以将 locationId 作为 URL 的一部分传递。 [HttpPost("GetCurrentLocationDateTime/locationId")] 并且不需要发布正文。您在客户端 ajax 请求上的 url 将是 "SpaEmployeeAdmin/GetCurrentLocationDateTime/" + locationId。【参考方案2】:
ajax
调用的 data
参数应格式化为 JSON 对象,如下所示:
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
);
按照惯例,服务器端 C# 期望请求的主体是 JSON,方法参数名称定义为 JSON 对象的属性。
【讨论】:
以上是关于将参数传递给 ajax 发布请求会返回错误的主要内容,如果未能解决你的问题,请参考以下文章
DataTables 警告:表 id=users - Ajax 错误。使用 jquery 将参数传递给它时
将额外参数传递给jquery ajax promise回调[重复]