tags:

views:

200

answers:

1

What is the point of doing this:

var resultsArray = (typeof response.d) == 'string' 
    ? eval('(' + response.d + ')') : response.d;

inside onSuccess() callback of $.(ajax) call?

A: 
var resultsArray =                  /* assign a value to resultsArray...        */
  (typeof response.d) == 'string' ? /* ...if the type of response.d is string   */
  eval('(' + response.d + ')')    : /* ...then evaluate it as if it was JS code */
  response.d;                       /* ...else just assign it unaltered         */
Tomalak
and the point of doing so is ....? ;-)
Sky Sanders
my question is actually what's the point of eval here? In what particular situation should web service return executable code????
gnomixa
@gnomixa: There are any number of possible reasons, returning evaluatable JavaScript is common practice for JSON web services, for example. It is entirely implementation dependent, and since I have no idea what `response.d` actually is in your context, I can hardly give an explanation.
Tomalak
i use this to communicate with asp.net service which always returns either: 1) string or 2)array of strings (JSON format).
gnomixa
my bad. brain fart. deleting now.
Sky Sanders
@gnomixa So I suppose the "else" part never really gets executed, since `response.d` is always of type string. (Unless there is some unusual condition when it is not, of course.)
Tomalak
^the other way around actually. i only use this statement when array of strings gets returned. I am centralizing all the calls to ajax method and hence i discovered that eval is useless to me completely.
gnomixa
@gnomixa: I'm not sure I understand this. JSON *always* is a string. You have toi pass it through `eval()` to make a JS data structure out of it. You don't get an "array of strings" from the server, but a string that *describes* an array of strings using a fixed format that happens to be valid JS syntax…
Tomalak
ok, so i am confused then. Some of my web services return array of strings (List<string>), some just a string. When I get it in my js code, since it's JSON format and always a string, I need to pass it through eval to get the array, correct?
gnomixa
I guess I confused because I tested my code and response.d is ALWAYS a string, even when List<string> is the web service called. So if JSON is ALWAYS a string, when would the eval part ever get executed? Am I missing something here?
gnomixa
@gnomixa: When your `List<string>` is rendered as JSON, it becomes a *textual representation* of an array of strings. When received by the browser, it ends up as a string of JavaScript compliant code (XmlHttpRequest response.text). You basically *must* pass it through `eval()` to make a data structure out of it, or it will stay a string with funny braces. Since I don't know where `response.d` comes from or what it may contain, I can only guess that it might not always contain a string, but `null` or `undefined` or something else entirely. In these cases the "else" part would be executed.
Tomalak
Thanks! I think I just found the issue. I thought my web service was returning JSON, but it's not. And I think I know why.I posted a related question herehttp://stackoverflow.com/questions/2851117/scriptmethodresponseformat-responseformat-jsonMy web service doesn't have response format = JSON. I will change it appropriately and will post on my progress.
gnomixa
@gnomixa I'm reading this as: We can consider this as answered. Your question is covered, and your actual problem seems to be something else completely. So can we please close this? :)
Tomalak
I am going to re-open this as according to msdn site default response format is JSON, so my web service does return json. Then i don't understand why (typeof response.d == 'string') is never true.Still confused.http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.aspx
gnomixa
@gnomixa: I don't understand what's so difficult on debuging this one line of code. Open the JS developer tools in your browser, set a frew breakpoints, look at variable values and find out. I'm not really sure how I can possibly help you any further, this can't be so hard after all.
Tomalak
It's not so much debugging as a question why things happen the way they do. I will open a new question.
gnomixa