0

I want to pass a list to Jquery and then use it with the Autocomplete jqueryui widget. It is a small list, so I don't think I need a new request. So, I guess I don't need to use Jsquery's getJSON.

I have:

 json_list = json.dumps(list)
 context = {'json_list':json_list}
 return render(request, template, context)

in jquery:

var json_list = JSON.parse({{json_list}});
$("#field").autocomplete({
  source: json_list
 });

I get a Syntax Error on "&quot":

 var autores_json = JSON.parse(["Friedrich Hayek", "Milton Friedma...

Im kind of lost here. Any help will be appreciated.

0

2 Answers 2

2

Forgot the quotes.

var json_list = JSON.parse('{{json_list}}');
$("#field").autocomplete({
  source: json_list
 });

Also, I'm not sure whether it will recognize " as a quote char, so maybe you should try:

var json_list = JSON.parse('{{json_list|safe}}');
$("#field").autocomplete({
  source: json_list
 });
Sign up to request clarification or add additional context in comments.

Comments

0

JSON.parse() need a string, so add some quotes.

Second, it's having trouble with HTML entities. After ensuring json_list is a string, use

JSON.parse(json_string.replace(/"/g,'"'));

EDIT: As mentioned above, better to pass safe in then use regex.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.