0

我在设置 jquery ajax 方法的 data 属性以包含一个 javascript 对象时遇到问题,该对象具有一个名为 EngineSpecs 的属性。

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

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

我不断收到如下错误:

Message":"发生错误。","ExceptionMessage":"无法反序列化当前 JSON 对象

任何帮助将不胜感激!

4

2 回答 2

1

您正在尝试将其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
        }]
   };
于 2013-04-09T16:08:17.973 回答
1

您的代码有几个问题似乎源于对 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
});

我希望这有帮助。

于 2013-04-09T16:57:42.143 回答