设置一个jquery ajax方法的data属性来包含一个带有数组属性的javascript对象

setting data property of a jquery ajax method to contain a javascript object with an array property

本文关键字:属性 包含一 数组 对象 javascript data 一个 jquery ajax 设置 方法      更新时间:2023-09-26

我在设置jquery ajax方法的数据属性以包含一个具有称为engineesec的属性的javascript对象时遇到了麻烦。

我想出了这样的东西来测试,但它不工作:

var myObject = new Object();
myObject.EngineSpecs = {};

var data = {
       myObject.EngineSpecs : [{
            EngineID: 100010017,
            Displacement: 7.2,
            Gas: false
            }, {
            EngineID: 300200223,
            Displacement:  3.2,
            Gas: true
       }]
};
$.ajax({
        url: someurl,
        dataType: "json",
        data: myObject

我一直得到这样的错误:

消息":"发生错误","ExceptionMessage":"不能反序列化当前JSON对象

任何帮助都将非常感激!

您正在尝试使用myObject.EngineSpecs作为对象属性名称,这是不允许的(因为。在中间)。这样做:

var data = {
       myObject: {
            EngineSpecs : [{
              EngineID: 100010017,
              Displacement: 7.2,
              Gas: false
            }, {
              EngineID: 300200223,
              Displacement:  3.2,
              Gas: true
            }]
       }
};

或者你真正想要的是:

  var myObject = {
        EngineSpecs : [{
          EngineID: 100010017,
          Displacement: 7.2,
          Gas: false
        }, {
          EngineID: 300200223,
          Displacement:  3.2,
          Gas: true
        }]
   };

你的代码有几个问题,似乎源于缺乏对js对象和对象属性的理解。理解这些将为你以后节省数小时的头痛。

我想指出的第一件事(一个小问题)是混合你的对象声明。

var myObject = new Object();
// is the equivalent of:
var myObject = {};

与数组类似:

var myArray = new Array();
//is the equivalent of:
var myArray = [];

你选择哪种模式并不重要(js社区更喜欢[]和{}),但要确保你的方法是一致的。

第二,将对象视为关联数组,即键->值对的列表。这些键被称为对象属性。所以所有的对象都应该遵循以下模式:

// Note: each object property declaration is comma separated.
var myObject = {
  propertyString: 'this is a string',
  propertyAnotherString: 'this is another string',
  propertyMixedArray: ['item 1', 'item 2', 1, 2, {}],
  // Note above, we have different data types in the array:
  // two strings, two ints and one empty object.
  // This is completely legal in javascript.
  propertyObject: { someProperty: 'and so on...' }
};
// Adding additional properties is OK too.
// Note: this time we use '=' instead of ':' to assign value
myObject.additionalProperty = 'Additional property';
// prints 'this is a string'
console.log(myObject.propertyString);
// also prints 'this is a string'
// I'm treating object as an associative array or hashtable
console.log(myObject['propertyString']);
// also prints 'this is a string'
// we can use variables as well to dynamically access keys.
var keyFromVariable = 'propertyString';
console.log(myObject[keyFromVariable]);
// Couple of other examples.
console.log(myObject.propertyMixedArray[1]); // prints 'item 2'
console.log(myObject.propertyObject.someProperty); // prints 'and so on...'
console.log(myObject.additionalProperty); // prints 'Additional property'

一开始这可能会让你不知所措,但随着时间的推移你会习惯的。然而,在编写javascript代码时,始终将上述想法牢记在心是很重要的。现在我们对对象有了更多的了解,我们可以用下面的方式重写代码:

var myObject = {
  EngineSpecs: [{
      EngineID: 100010017,
      Displacement: 7.2,
      Gas: false
      }, {
      EngineID: 300200223,
      Displacement:  3.2,
      Gas: true
  }]
};
$.ajax({
  url: 'http://www.someurlhere.com/api.json',
  dataType: "json",
  data: myObject
});