Passing javascript object by parameter in ajax callback

2

I know to pass string:

onclick="popularEdicaoRetrofit(\''+data[i]+'\');"

and number:

var criarRetrofit = onclick="criarRetrofit('+data[i].idRetrofit+');"

I would like to know how to pass an object as a parameter to the javascript function. The code is generated in% AJAX% return.

The code is this in the image below (i is the position of the vector being traversed by for):

    
asked by anonymous 02.07.2015 / 15:36

4 answers

5

The solution is simple:

Just do this:

onclick="popularEdicaoRetrofit(meuNamespace.data);"

But on the back of the dice via Ajax do this:

var meuNamespace = meuNamespace || {};
meuNamespace.data = meuObjetoDataRecebidoDoServidorRemoto;

In this approach you are saving the data in a global object that I called from myNamespace which is not a very good solution but should answer in your case.

Ideally, rewrite the code to make it more robust.

    
02.07.2015 / 17:02
3
___ erkimt ___ Passing javascript object by parameter in ajax callback ______ qstntxt ___

I know to pass string:

var botaoMaisInformacoes = document.createElement('button');
(function(){
   var index = i;
    botaoMaisInformacoes.addEventListener('click', function(){
       popularEdicaoRetrofit(data[i]);
    });
})();

and number:

'onclick = "popularEdicaoRetrofit(\'' + JSON.stringify(data[i]) + '\');"'

I would like to know how to pass an object as a parameter to the javascript function. The code is generated in% AJAX% return.

The code is this in the image below (i is the position of the vector being traversed by for):

    
______ azszpr72456 ___

The solution is simple:

Just do this:

var objeto = JSON.parse(dataString);

But on the back of the dice via Ajax do this:

var botaoMaisInformacoes = document.createElement('button');
(function(){
   var index = i;
    botaoMaisInformacoes.addEventListener('click', function(){
       popularEdicaoRetrofit(data[i]);
    });
})();

In this approach you are saving the data in a global object that I called from myNamespace which is not a very good solution but should answer in your case.

Ideally, rewrite the code to make it more robust.

    
______ ___ azszpr72448

When you use inline JavaScript in HTML this way you are limited. It would be better criares the object with JavaScript and join one oscultador events where you can spend the type of object you want.

Notice that there is another problem here the bfavaretto noticed and I do not . You can read more about the issue here , but deep down this i % must be saved in memory not to be overridden by the loop.

So I suggest something of the style:

'onclick = "popularEdicaoRetrofit(\'' + JSON.stringify(data[i]) + '\');"'

instead of onclick = "popularEdicaoRetrofit(\''+data[i]+'\');"

However If you are using objects that can be converted into JSON can do

var objeto = JSON.parse(dataString);

So the object is passed as JSON / string and within the function you only have to do

%pre%     
______ azszpr73567 ___

Good evening guys, I ended up passing the parameters in this simplest way:

%pre%     
______ azszpr72440 ___

Hello, Felipe.

JavaScript is not a strongly typed language, which allows you to receive any object per parameter in its function. Apparently you can pass the %code% object itself, but check the format of that object to make sure it's what you want.

    
___
02.07.2015 / 16:36
0

Good evening guys, I ended up passing the parameters in this simplest way:

'<td align="center"><button class="btn btn-default" data-toggle="modal" data-target="#editarRetrofit" onclick="popularEdicaoRetrofit('+data[i].idRetrofit+',\''+data[i].nomeRetrofit+'\'
    
09.07.2015 / 02:39
-1

Hello, Felipe.

JavaScript is not a strongly typed language, which allows you to receive any object per parameter in its function. Apparently you can pass the data object itself, but check the format of that object to make sure it's what you want.

    
02.07.2015 / 16:10