javascript Unexpected illegal token

Hi I get "Uncaught SyntaxError: Unexpected token ILLEGAL" error, when I run this code

str += "{'value': "+ response_imdb[count] +",
      'color':#"+ colorValue +",
      'label': "+ response_labels[count] +"
     }";

Thanks.


这是另一个简单的方法。

str += JSON.stringify({
  value: response_imdb[count],
  color: '#' + colorValue,
  label: response_labels[count]
});
         

In JavaScript you cannot have multi-line strings (unless you add a backslash to the end of each line).

Make them multiple strings and concatenate them using + , like so:

str += "{'value': "+ response_imdb[count] +"," +
  "'color':#"+ colorValue +"," +
  "'label': "+ response_labels[count] +
 "}";

Javascript doesn't allow line breaks in strings. You heave line breaks after ", at the end of each line. You should change it to:

str += "{'value': "+ response_imdb[count] +",n"+
      'color':#"+ colorValue +",n"+
      'label': "+ response_labels[count] +",n"+
     }";

But it's almost always wrong to try to create JSON strings by hand. Use a function for it, like JSON.stringify in Javascript, json_encode in PHP, etc.

There are some other problems there. If the string is going to be parsed as JSON, it the property names need to be in double quotes, not single quotes. And # + colorValue needs to be in quotes to be a string.

链接地址: http://www.djcxy.com/p/58084.html

上一篇: Eclipse按键可以左右滑动文本块

下一篇: javascript意外的非法令牌