Execute function passed by json

0

I have this ajax function:

function GuardarJson(url,que,id,cod,param,respuesta) {
    var Daticos = {id: id ,codigo: cod, respu: respuesta };
    $.ajax({
    url: "Paginas/"+url+"?que="+param,
        data: Daticos,
        type: "post",
        dataType: 'json',
        success: function(data) {
            Alerta(data.alerta,data.Tipo,'');
            data.script
            },
        error: function(xhr, status, error){ $().toastmessage('showToast', { text : 'Error '+ xhr.responseText, sticky : true, type : 'error' }); }
    });
};

The fact is that from json I pass a series of data and one of them is script inside script I want to pass functions to execute variables according to which case.

How can I make that text that passed through json be executed as a function?

the format that I'm passing is this:

$Respuesta['script'] = "$(\"#MenuIdFactura_".$Que."\").val('".$IdNuevo."').change(); alert('Holiii');";

the variables of the answer are because it is inside a php function but when they run they arrive with the data well placed.

    
asked by Killpe 25.12.2017 в 23:14
source

2 answers

1

You can use the eval function of javascript that evaluates a string and executes it:

eval('$("#input-id").val(4)');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="input-id" />

In your case, you would only pass the string of the server's response.

Now, if you search the internet about eval, you will see articles that prohibit its use. So in your case, what I would do would be to send the input id, and the value that you want to assign to ajax, and so you save sending other data in the server's responses.

For example let's say that now you return the input id and the value, then you will only change this:

function GuardarJson(url,que,id,cod,param,respuesta) {
    var Daticos = {id: id ,codigo: cod, respu: respuesta };
    $.ajax({
    url: "Paginas/"+url+"?que="+param,
        data: Daticos,
        type: "post",
        dataType: 'json',
        success: function(data) {
              $("#MenuIdFactura_"+data.id_input).val(data.nuevo_valor);
            },
        error: function(xhr, status, error){ $().toastmessage('showToast', { text : 'Error '+ xhr.responseText, sticky : true, type : 'error' }); }
    });
};
    
answered by 25.12.2017 / 23:31
source
1

Einer has already given you the option to use eval has also advised you to refactor the logic of your denial so that you follow good practices.

However, if you still want to inject javaScript even after everything you have read so far, you could also do it in the following way:

;(function() {

var script = document.createElement('script')
var script_js = "alert('hola mundo')"

script.appendChild(
  document.createTextNode(script_js)
)

document.body.appendChild(script)
document.body.removeChild(script)

}())

The logic is simple, a HTMLElement is created that coincides with HTMLScriptElement , itself is injected with the code through a Text element. Once the Script with the content to be evaluated has been created, it is added to DOM delegating the evaluation of it. The evaluation is synchronous so after adding to the DOM (and therefore evaluating it) it is eliminated from DOM (it is no longer necessary)

    
answered by 25.12.2017 в 23:55