1

I have a $.POST call that return the name of a function that need to be ran but it wont execute the function and I don't know why.

Here is an example:

JS file:

$(function(){
     $.post('test.php',{event: 'add'},
          function(data){
               data.func(data.msg);
          },'json');

     function test(msg){
          alert(msg);
     }
});

PHP Ajax:

<?php
     switch($_POST['event']){
          case 'add':
               $output['func'] = 'test';
               $output['msg'] = 'This is add message';
               break;
          case 'delete':
               $output['func'] = 'test';
               $output['msg'] = 'This is delete message';
               break;
     }
     echo json_encode($output);
 ?>

The problem I am having is the ajax is returning the name of the function (test) but it will not run the function, how do I fix this?

0

2 Answers 2

6

DO NOT USE EVAL.

Rather, make an object with the functions you want to be executable. For example:

var functionTable = {
    test: function (msg) {
        alert(msg);
    }
};

$.post('test.php', { event: 'add' }, function (data) {
    if (functionTable.hasOwnProperty(data.func)) {
        functionTable[data.func](data.msg);
    }
}, 'json');
Sign up to request clarification or add additional context in comments.

7 Comments

Great, this is exactly the same as I have ;) +1
@Harmen, Yup, I saw that soon after I posted my answer. I prefer to use hasOwnProperty, however. (Who knows; someone may add a dangerous function to Object.prototype which gets called remotely...
Eval() is only as "evil" as your usage of it. If he's generating that JSON in a way that's isolated from user generated data (which it appears he is), there's not much pragmatic reason to avoid eval(). It's particularly unnecessary to avoid it in this case, because $.post will already be using an eval() derivative to deserialize the JSON response in all but the most current browsers anyway.
@Dave Ward, jQuery will use the JSON.parse() function to parse JSON. And still, you cannot be sure that the output is a JSON structure with a func and msg key
@Harmen: Only in newer browsers that have implemented that. There are still an awful lot of browsers currently in use (anything below IE8, Firefox 3.5, Chrome 3, and Safari 5, I believe) that don't implement JSON.parse natively. In those browsers, jQuery uses new Function(json) to deserialize JSON, which is effectively an eval().
|
2

I think it's better to move your function into an object here, so you can see if it exists or not:

var possibleFunctions = {
  test: function(val){
    alert(val);
  }
};

$(function(){
     $.post('test.php',{event: 'add'},
          function(data){
              if(possibleFunctions[data.func]){
                  possibleFunctions[data.func](data.msg);
              }
          },'json');
      });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.