简体   繁体   中英

My javascript code does not work if there are linebreaks in a var?

var a = {"name":"ab
ac", age="112"};
alert(typeof a);

My code does not run. However if i change it to

var a = {"name":"abac", age:"112"};
alert(typeof a);

It runs properly. But the problem is that this var a I get from a GET call to a url and it appears as this only.Is there a way i can change it in code to get it working? I am new to Javascript.

I make a get call to a url. the responseText i get is

    var a = {"name":"ab
ac", "age":112};

whereas i want it to be of type

 var a= {"name":"abac", age="112"}; 

because the above a is not being recognized by javascript. My question is i cannot manually put a "\\" char in the resposeText since all that is happening inside code. Is there some code that can do it for me?

Use line-continuation "\\" symbol as the last character on the line before the break:

var a = {"name":"ab\
ac", age:"112"};

There are two problems

var a = {"name":"ab \ 
ac", age:"112"}; //notice : in place of =
alert(typeof a); 
  1. You need to add \\ in case a string has to be continued to next line.

  2. Key value has to be separated by : not =

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM