简体   繁体   English

设置jquery ajax方法的数据属性以包含具有数组属性的javascript对象

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

I'm having trouble setting the data property of a jquery ajax method to contain a javascript object that has a property called EngineSpecs. 我无法设置jquery ajax方法的data属性来包含一个具有名为EngineSpecs属性的javascript对象。

I came up with something like this to test, but it's not working: 我想出了这样的东西来测试,但它不起作用:

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

I keep getting an error like: 我一直收到如下错误:

Message":"An error has occurred.","ExceptionMessage":"Cannot deserialize the current JSON object 消息“:”发生错误。“,”ExceptionMessage“:”无法反序列化当前的JSON对象

Any help would be greatly appreciated! 任何帮助将不胜感激!

You are attempting to use myObject.EngineSpecs as an object property name, which isn't allowed (because of the . in the middle). 您正在尝试将myObject.EngineSpecs用作对象属性名称,这是不允许的(因为中间的。)。 Do this instead: 改为:

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

or possibly what you really wanted was: 或者你真正想要的是:

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

There are several problems with your code which seem to stem from lack of understanding of js objects and object properties. 您的代码存在一些问题,这些问题似乎源于对js对象和对象属性缺乏了解。 Understanding these will save you hours of headache later on. 了解这些将为您节省数小时的头痛时间。

First thing (a minor one) I would like to point out is mixing of your object declarations. 我要指出的第一件事(一个小问题)是混合你的对象声明。

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

Similar with arrays: 与数组类似:

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

It doesn't matter which pattern you choose to go with (js community prefers [] and {}), but be sure you are consistent with your approach. 选择使用哪种模式无关紧要(js社区更喜欢[]和{}),但请确保您的方法与您的方法一致。

Second, think about objects as associative arrays, ie list of key -> value pairs. 其次,将对象视为关联数组,即键 - >值对的列表。 These keys are referred to as object properties. 这些键称为对象属性。 So all objects should follow the following pattern: 所以所有对象都应遵循以下模式:

// 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'

This might be overwhelming initially, but you get used to it over time. 这可能最初是压倒性的,但随着时间的推移,你会习惯它。 It's important however to keep the ideas above in the back of your mind at all times while writing javascript code. 然而,在编写JavaScript代码时始终将这些想法始终放在脑海中是非常重要的。 Now that we know a little more about objects, we can rewrite your code in a following way: 现在我们对对象有了更多了解,我们可以通过以下方式重写代码:

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
});

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM