Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
Probably needs eval.... – Pekka 웃 Nov 6 '10 at 12:26

2 Answers

up vote 6 down vote accepted

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');
share|improve this answer
Great, this is exactly the same as I have ;) +1 – Harmen Nov 6 '10 at 12:34
@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... – strager Nov 6 '10 at 12:36
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 Nov 6 '10 at 13:04
@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 Nov 6 '10 at 13:07
@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(). – Dave Ward Nov 6 '10 at 13:19
show 2 more comments

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');
      });
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.