English 中文(简体)
javascriptites
原标题:javascript array mistake

我在js中制造了一个阵列。

var data = new Array();  
data[ id ] = self.iframeFields.id.val();  
data[ name ] = self.iframeFields.name.val();  
data[ location ] = self.iframeFields.location.val();  
data[ about ] = self.iframeFields.about.val();  
data[ company ] = self.iframeFields.company.val();  
data[ website ] = self.iframeFields.website.val();

但通过<条码> 瓦尔数据

but data[ id ] return value.

What did I do wrong?

EDIT: After nrabinowitz s answer, i was using

if ($.isArray( data )){ ajax({

        url: myurl,
        data: {
            method: "updateProfile",
            data: data
        },
        normalizeJSON: true,
        success: function( response ){
            // Check to see if the request was successful.
            if (response.success){
                alert(response);
            } else if (onError){
                // The call was not successful - call the error function.
                alert(response);
            }
        }
    });
       }

因为它不是一个阵列,

它没有归还任何东西。

驱逐

  if ($.isArray( data )){
        }

解决这个问题。

最佳回答

在 Java文中,你想要一个物体,而不是一个阵列:

var data = {};
data[ id ] = self.iframeFields.id.val();
// etc...

你们期望这些阵列能够像PHP中的一个联系阵列一样工作,但 Java类阵列并没有这样做。 我假设你重新确定这些价值,然后试图通过像“<<>>>><>>>>>>>>>代号”这样的阵列,但当你can<>/em>通过钥匙设定了价值时,因为 Java类阵列只是另一个目标,这些数值在标准阵列中获胜,阵列的长度仍然为0。

http://www.un.org。 请注意,您重新使用j Query s.ajax()功能将数据张贴到服务器。 <代码>.ajax(”方法预期含有关键/价值乳制品的物体,并将这些物体作为GET或POST参数发送给服务器。 因此,如果您重新使用上文描述的物体,其服务器将接收<代码>id”、、> 姓名<>/code>等在_$_POST range- a >>data>参数。

我怀疑,尽管我有点对此进行了测试,但使用<代码>var data = new Array(); ben t work at all,因为将数据编成.ajax()——尽管一个阵列也是一个物体,但如果它有阵列和treats it differently:

如果所通过物体为阿雷拉,它必须是由宇宙射线(Array)归还的系列物体。

[{name:"first",value:"Rick"}, {name:"last",value:"Astley"}, {name:"job",value:"Rock Star"}]

因此,它根本不使用你确定的关键/价值配对机,你将穿过空阵,任何参数都不会传给服务器。

因此,这里的正确做法是:

  1. Use var data = {};
  2. On the server, look for $_POST[ id ], $_POST[ name ], etc.
问题回答

你再次使用像固定物体这样的阵列。 你用额外特性补充阵列,但阵列本身仍然空(应当有阵列)。 页: 1 变化

var data = new Array();

to

var data = {};

并了解你们获得的东西。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签