0

What is the point of doing this:

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

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

sarsnake
  • 26,667
  • 58
  • 180
  • 286

1 Answers1

0
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
  • 332,285
  • 67
  • 532
  • 628
  • my question is actually what's the point of eval here? In what particular situation should web service return executable code???? – sarsnake May 14 '10 at 22:26
  • @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 May 14 '10 at 22:55
  • i use this to communicate with asp.net service which always returns either: 1) string or 2)array of strings (JSON format). – sarsnake May 14 '10 at 23:13
  • @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 May 14 '10 at 23:59
  • ^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. – sarsnake May 15 '10 at 00:29
  • @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 May 15 '10 at 06:41
  • ok, so i am confused then. Some of my web services return array of strings (List), 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? – sarsnake May 17 '10 at 16:44
  • I guess I confused because I tested my code and response.d is ALWAYS a string, even when List 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? – sarsnake May 17 '10 at 16:50
  • @gnomixa: When your `List` 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 May 17 '10 at 17:48
  • 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 here http://stackoverflow.com/questions/2851117/scriptmethodresponseformat-responseformat-json My web service doesn't have response format = JSON. I will change it appropriately and will post on my progress. – sarsnake May 17 '10 at 18:32
  • @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 May 17 '10 at 18:49
  • 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 – sarsnake May 18 '10 at 00:03
  • @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 May 18 '10 at 00:19
  • It's not so much debugging as a question why things happen the way they do. I will open a new question. – sarsnake May 18 '10 at 00:25