Javascript string for variable

I get a JSON string from an ajax call and would like to convert the value to a predefined variable:

var predefined = "hello world"; var foo = {"msg":"predefined"}; // JSON string 

I want to highlight a standard string by accessing it with

 alert(foo.msg) 

EDIT: to make the answer clearer, this is my call:

 var success_msg = "Your email is send successfully!"; $.ajax({ url: "ajax-share-email.php", type: "POST", dataType: "json", data: {}, success: function(data) { if (data.status == "success") { msg.text(data.msg).addClass("email-msg-success"); } else { msg.text(data.msg).addClass("email-msg-error"); } } }) 

ajax-share-email.php answers:

 {"status":"success", "msg":"success_msg"} 
+4
source share
5 answers
 var strings = {"predefined":"hello world"}; alert(strings[foo.msg]); 

or for example

 var messages = {}; messages.success_msg = "Your email is send successfully!"; // ... msg.text(messages[data.msg]).addClass("email-msg-success"); 
+5
source

How about this - just use the inline on success message and don’t even bother to make it part of JSON. On error, include the entire message and use it directly. In addition, I will have a server returning something like:

 { "status": true } 

or

 { "status": false, "msg": "The mail server is down." } 

Then you can simply evaluate it as a logical one without comparing it to a string value.

 $.ajax({ url: "ajax-share-email.php", type: "POST", dataType: "json", data: {}, success: function(data) { if (data.status) { msg.text('Your email has been sent successfully!').addClass("email-msg-success"); } else { msg.text(data.msg).addClass("email-msg-error"); } } }); 

If and only if you start to reuse your messages for several functions, then refactoring to the message dictionary and a link to it from there. Note that your messages object should probably be a global variable, or at least in the outer scope of all functions that use it.

  var messages = {}; messages.mail_success = 'Your email has been sent successfully!'; messages.post_success = 'Your data has been updated!'; $.ajax({ url: "ajax-share-email.php", type: "POST", dataType: "json", data: {}, success: function(data) { if (data.status) { msg.text(messages.mail_success).addClass("email-msg-success"); } else { msg.text(data.msg).addClass("email-msg-error"); } } }); 
+2
source
  var predefined = "hello world";
 var foo = {"msg": predefined};  // JSON string
 alert (foo.msg)

?

0
source
 var out = eval(foo.msg); // out is now "hello world" 

Note. Do not use eval() unless you are sure what the content of foo.msg .

or

 var out = foo.msg=="predefined" ? predefined : foo.msg; 
0
source

IFF I understand what you are asking, I think I have all the parts.

You have a predefined variable, and you want to return it to your json and get the resulting parsed object in predefined

JSON.parse will not work for you (at least not in Chrome), but eval will.

 var predefined = "Hi! I'm predefined"; // ... var json = '{"foo": predefined}'; // notice no quotes var response = eval("(" + json + ")"); alert(response.foo); 
0
source

Source: https://habr.com/ru/post/1334819/


All Articles