Pass javascript object by parameter in ajax callback

Asked

Viewed 1,708 times

2

I know how to string:

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

and number:

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

I would like to know how to step an object as a parameter of the javascript function. The code is generated on the return of the callback ajax.

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

trecho do código

funcao que irá receber o objeto

  • Could you please post the code of what you have done so far?

  • 3

    Felipe, please post the code as text (then select and type CTRL+K to format as code).

4 answers

5


The solution is simple:

just do it like this:

onclick="popularEdicaoRetrofit(meuNamespace.data);"

But on the turn 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 meuNamespace which is not a very good solution but should meet in this your case.

The ideal is to rewrite the code to make it more robust.

3

When you use Javascript inline in HTML that way you’re limited. It would be better to create the object with Javascript and join an event receiver where you can pass the type of object you want.

Notice there’s another problem here that the bfavaretto noticed and I did not. You can read more about the problem here, but deep down i must be stored in memory so that it is not overwritten by the loop.

So I suggest something of the style:

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

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

However if you are using Objects that can be converted to JSON you can do

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

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

var objeto = JSON.parse(dataString);
  • I think that this data is only available in ajax callback, and is being looped into it. So none of these alternatives should work.

  • @bfavaretto I also think so, but in the first picture of the question you can see the ess: that I think is the rest of the word "Success" of callback. That is, that code is running inside the callback.

  • In that case it would still need an extra closure to prevent i always be the last value.

  • @bfavaretto well seen. I added this to my suggestion to use addEventListener to the other the JSON.stringify already uses the i in place. Thank you!

0

Good night, you guys, I just happened to pass the parameters in that simpler way:

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

-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 object itself data, however check the shape of this object to make sure that is what you want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.