tags:

views:

60

answers:

5

Here is the problem string:

$.ajax(......
   ,success:function(msg){
      var obj = eval('['+msg.d+']');
   },
   ....
}

msg.d contains something like:

new Person(2, 'Name Surname','This is just string came from Par'is at Sat'urday');

How can pass the javascript problem ?

+1  A: 

Using eval in this scenario is actual quite dangerous. You really ought to be using XML or JSON. (That's why they call it AJAX.)

Eric Mickelsen
To be clear, the J in AJAX is for JavaScript, not JSON...and a string fits under that category.
Nick Craver
@Nick, I was referring to the X. And the J doesn't refer to using Javascript code as a communication medium via eval.
Eric Mickelsen
@Eric - JSON doesn't provide a way to pass an executable function either, so what would it directly solve here?
Nick Craver
@Nick, There's no mention of passing functions in the message (in the OP). The example is just a simple object, which JSON is well-suited for. If anyone has an urge to pass a function as a message, I would reconsider that, since it is likely a major design flaw.
Eric Mickelsen
@Eric - The object could be any of 30 types for example, maybe he gets a Person, or a Class or a Car...you're making some *assumptions* here that it's an easy thing to bypass :)
Nick Craver
@Nick, Granted, I made an unstated assumption. I'm making a normative recommendation on the basis of one example. The OP makes no attempt to specify the domain of possible messages. However, I doubt that it's such an unreasonable assumption that every recommendation of JSON should have a big "CAN'T SERIALIZE FUNCTIONS!" sign on it. Frankly, I'm just not very concerned with the possibility of being sued over omitting that disclaimer.
Eric Mickelsen
+5  A: 

If the string really is as you've quoted it, it has a syntax error and will not work (it has an errant ' inside the word "Saturday"). Otherwise, though, change the brackets ([ and ]) in your eval call to parentheses (( and )):

var obj = eval('('+msg.d+')');

However, it should almost never actually be necessary to do this (or indeed to use eval at all). It's almost always possible, and desirable, to refactor slightly and avoid it.

If that's a literal quote from your code, see also dvhh's answer below, your function argument name (msg.d) is invalid.

T.J. Crowder
A: 

I would avoid using eval() for security reasons. If a user can get malicious code into the database, there's a chance it could end up in this eval expression, wreaking havoc for anybody who visits this page.

Instead of using eval, I'd recommending returning JSON from the AJAX request. You can then easily parse the values and build a new Person object with that data.

Colin O'Dell
+1  A: 

the function argument should be a valid javascript identifier

try changing msg.d to msg_d for example

dvhh
Well spotted...
T.J. Crowder
+1  A: 

You may need to escape your string, because this example works fine:

function MyObject(myvar){
    this.hello = function(){
        alert('myvar= ' + myvar);
    };
}

var obj1 = new MyObject('hello');
obj1.hello();

var obj2 = eval("new MyObject('world')"); 
obj2.hello();

(Edit: By the way, I assume msg.d is a typo due to editing the snipplet before posting on StackOverflow ?)

wildpeaks
If you have control over the server-side, I would advise to use JSON (or XML, but it slightly slower), that makes things more maintainable and it's easy to use in (I assume you use jQuery due to the $.ajax), just change the "dataType" field to 'json' and you get directly receive a nice structure in the success handler).
wildpeaks