gpt4 book ai didi

jquery - 无效的 JSON 原语 : object

转载 作者:行者123 更新时间:2023-12-01 01:00:52 27 4
gpt4 key购买 nike

我正在尝试通过以下 jQuery Ajax 调用将字符串化的 JSON 对象发送到 MVC 方法:

$.ajax({
type: "POST",
url: "UpdateItem",
contentType: "application/json; charset=utf-8",
dataType: "json",
processData : false,
data:
{
item: JSON.stringify(_item)
},
success: function (data) {
alert(data);
},
error: function (x, t, m, b) {
DisplayErrorMessage(x.responseText);
}
});

我的数据的字符串化版本如下:

{
"Id": 4,
"ParentId": 1,
"TypeId": 2,
"TypeText": "Solid",
"ItemNo": 8,
"StandartTypeId": 7,
"StandartTypeText": "Dept",
"GradeTypeId": 6,
"GradeTypeText": null,
"Thickness": 0.044,
"ThicknessToleranceId": 1,
"ThicknessToleranceText": null,
"Width": 42,
"MinWeightId": 6,
"MinWeight": null,
"MinWeight2": null,
"MaxWeightId": 8,
"MaxWeight1": null,
"MaxWeight2": null,
"DefId": null,
"Quantity": 330690,
"QuantityToleranceId": 3,
"QuantityToleranceText": "",
"ProductionDate": "2014-11-05T22:00:00.000Z",
"PortId": 3,
"PortText": null,
"DeliveryDate": "2014-10-08T21:00:00.000Z",
"MaterialTypeId": 2,
"MaterialTypeText": "",
"FeePrepaid": 30,
"Price": 525,
"Extra1": 0,
"Extra1": 0,
"CurrencyId": 2,
"CurrencyText": "",
"StatusId": 2,
"StatusText": "",
"ReasonId": null,
"ReasonText": null,
"Note": "New note",
"CreateDate": "2014-11-06T09:12:29.661Z",
"CreateUserId": 0,
"CreateUserText": "",
"CancelDate": null,
"CancelUserId": null,
"CancelUserText": null,
"ChemicalProperties": null,
"TechnicalProperties": null,
"Remarks": null
}

我已通过 http://jsonlint.com/ 成功验证了我的 JSON 对象.

我尝试通过以下方法获取响应:

public JsonResult UpdateItem(string json)
{
var js = new JavaScriptSerializer();
var deserializedItem = (object[])js.DeserializeObject(json);


return Json(null);
}

但是当我尝试通过 Ajax 发布时,在 ASP.NET MVC Controller 方法调用之前收到以下错误消息:

Invalid JSON primitive: object. Exception Details: System.ArgumentException: Invalid JSON primitive: object.

我的堆栈跟踪如下:

[ArgumentException: Invalid JSON primitive: object.]
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() +915
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +597
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeList(Int32 depth) +354
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +531
System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) +108
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +210
System.Web.Script.Serialization.JavaScriptSerializer.DeserializeObject(String input) +86
System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext) +191
System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +19
System.Web.Mvc.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory) +34
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +145
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +171
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +460
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) +281
System.Web.Mvc.ControllerBase.get_ValueProvider() +40
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +60
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +117
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +446
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +302
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +30
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +382
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +317
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +15
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +71
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +249
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

最佳答案

你尝试过:

data: JSON.stringify(_item)

只是一个猜测,但我认为使用:

data:
{
item: JSON.stringify(_item)
}

不会给你你想要的东西。

由于您正在 Controller 中等待 String,因此应在请求中传递 String

如果你想要多个对象,你必须创建一个变量来转换为如下内容:

JSON.stringify(_items) => "[{id:1},{id:2}]";

然后在:data: JSON.stringify(_item)

中使用它

关于jquery - 无效的 JSON 原语 : object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26776104/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com