为什么在我进行 post 调用时我的操作方法没有被触发?
Why my action method not triggered when I make post call?
我使用 MVC ASP。
这是我发送给操作方法的 javascript 对象:
var layerProp = {VectorLayerId:5, FieldName: "someText", FieldType:"someText", FieldValue: "someText", Required:false }
这是我的 ajax 函数:
function saveProperty(layerProp, callback, error) {
return $.ajax({
url: '/Mobile/LayerProperty/SaveProperty',
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { layerProp: layerProp },
success: callback,
error: error
});
}
这是我在控制器中的操作类型:
[HttpPost]
public JsonResult SaveProperty(GeomindMobile.Core.Data.VectorLayers_Fields layerProp)
{
try
{
var q = _repository.SaveProperty(layerProp);
return Json(new { Result = "OK" }, JsonRequestBehavior.DenyGet);
}
catch (Exception ex)
{
return Json(new { Result = "Bad", ex.Message });
}
}
这是我的类型定义的实体:
public partial class VectorLayers_Fields
{
public VectorLayers_Fields()
{
this.VectorLayer_FieldsValue = new HashSet<VectorLayer_FieldsValue>();
}
public int Id { get; set; }
public int VectorLayerId { get; set; }
public string FieldName { get; set; }
public string FieldType { get; set; }
public string FieldValue { get; set; }
public bool Required { get; set; }
public virtual ICollection<VectorLayer_FieldsValue> VectorLayer_FieldsValue { get; set; }
}
知道为什么当我 post 我的数据到控制器时会出现此错误:
无法加载资源:服务器响应状态为 500(内部服务器错误)
更新:
这是关于我在 fiddler 中遇到的错误的消息:
[ArgumentException: Invalid JSON primitive: layerProp.]
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() +707
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +333
System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) +110
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +46
System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext) +230
System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +24
System.Web.Mvc.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory) +45
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +223
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +263
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +504
System.Linq.Enumerable.ToList(IEnumerable`1 source) +70
System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) +361
System.Web.Mvc.ControllerBase.get_ValueProvider() +50
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +77
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +143
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +744
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +164
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +380
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +44
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +164
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +532
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +164
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +416
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +120
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +164
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +309
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1028
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +134
在您的 JS AJAX 调用中,尝试更改数据 属性 来自:
data: { layerProp: layerProp },
收件人:
data: JSON.stringify(layerProp),
您犯的错误是您提供的数据是一个带有 属性 "layerProp" 的对象。因此,如果我们将其编写为 C# class,您将得到:
class Whatever
{
public SomeType LayerProp { get; set; }
}
您在控制器上期望的类型是具有所有值的对象,例如 FieldName、FieldType 等。因此在 JSON 格式中,您的 JSON 对象的开头(第一个{), 应该包含这些属性。如:
data: { 'Id': 10, FieldName: 'Hello', FieldType: 'World'....
JSON.stringify 是所有浏览器中的辅助函数,可以安全地将 JS 对象中的 values/symbols 转换为正确的 JSON 格式。例如,它将获取一个 JS Date 对象并将其转换为带有时区信息的有效 date/time 字符串。尝试从 JS 对象创建 JSON 字符串时,只是 easier/safer 使用它。更多信息在这里:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
希望对您有所帮助。
我使用 MVC ASP。
这是我发送给操作方法的 javascript 对象:
var layerProp = {VectorLayerId:5, FieldName: "someText", FieldType:"someText", FieldValue: "someText", Required:false }
这是我的 ajax 函数:
function saveProperty(layerProp, callback, error) {
return $.ajax({
url: '/Mobile/LayerProperty/SaveProperty',
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { layerProp: layerProp },
success: callback,
error: error
});
}
这是我在控制器中的操作类型:
[HttpPost]
public JsonResult SaveProperty(GeomindMobile.Core.Data.VectorLayers_Fields layerProp)
{
try
{
var q = _repository.SaveProperty(layerProp);
return Json(new { Result = "OK" }, JsonRequestBehavior.DenyGet);
}
catch (Exception ex)
{
return Json(new { Result = "Bad", ex.Message });
}
}
这是我的类型定义的实体:
public partial class VectorLayers_Fields
{
public VectorLayers_Fields()
{
this.VectorLayer_FieldsValue = new HashSet<VectorLayer_FieldsValue>();
}
public int Id { get; set; }
public int VectorLayerId { get; set; }
public string FieldName { get; set; }
public string FieldType { get; set; }
public string FieldValue { get; set; }
public bool Required { get; set; }
public virtual ICollection<VectorLayer_FieldsValue> VectorLayer_FieldsValue { get; set; }
}
知道为什么当我 post 我的数据到控制器时会出现此错误:
无法加载资源:服务器响应状态为 500(内部服务器错误)
更新:
这是关于我在 fiddler 中遇到的错误的消息:
[ArgumentException: Invalid JSON primitive: layerProp.]
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() +707
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +333
System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) +110
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +46
System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext) +230
System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +24
System.Web.Mvc.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory) +45
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +223
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +263
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +504
System.Linq.Enumerable.ToList(IEnumerable`1 source) +70
System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) +361
System.Web.Mvc.ControllerBase.get_ValueProvider() +50
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +77
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +143
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +744
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +164
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +380
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +44
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +164
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +532
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +164
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +416
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +120
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +164
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +309
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1028
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +134
在您的 JS AJAX 调用中,尝试更改数据 属性 来自:
data: { layerProp: layerProp },
收件人:
data: JSON.stringify(layerProp),
您犯的错误是您提供的数据是一个带有 属性 "layerProp" 的对象。因此,如果我们将其编写为 C# class,您将得到:
class Whatever
{
public SomeType LayerProp { get; set; }
}
您在控制器上期望的类型是具有所有值的对象,例如 FieldName、FieldType 等。因此在 JSON 格式中,您的 JSON 对象的开头(第一个{), 应该包含这些属性。如:
data: { 'Id': 10, FieldName: 'Hello', FieldType: 'World'....
JSON.stringify 是所有浏览器中的辅助函数,可以安全地将 JS 对象中的 values/symbols 转换为正确的 JSON 格式。例如,它将获取一个 JS Date 对象并将其转换为带有时区信息的有效 date/time 字符串。尝试从 JS 对象创建 JSON 字符串时,只是 easier/safer 使用它。更多信息在这里:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
希望对您有所帮助。