Just created a new project on ASP.NET Core 2.0. Connect all the necessary libraries. JQoure works without problems. But it is in this method does not go. The api controller normally returns the value. As in my previous all the same.

<script> function getData(data) { //var x = document.getElementById("brand"); //var option = document.createElement("option"); //option.text = "Kiwi"; //x.add(option); var url = "/api/CarData"; $.getJSON(url, function (data) { var obj = $.parseJSON(data); var x = document.getElementById("brand"); var option = document.createElement("option"); option.text = "hello"; x.add(option) }); } $(document).ready(function () { getData(); }); 

  <form> <select id="brand" size="8"> <option>Apple</option> </select> </form> 

The selection itself is located if the lines above are uncommented. But why doesn’t it go into the gedjson method ... xs ... All libraries are connected. He did the same as in my previous project and there everything plows. What did I do?

  • "The api controller returns normally." - what? What will come if you enter the url of the request in the address bar of the browser? - Igor

1 answer 1

Since you call $.getJSON , do $.parseJSON(data); - it does not follow. data is already a js object.

 $.getJSON(url, function (data) { //var obj = $.parseJSON(data); var obj = data; console.log(typeof obj, obj); var x = document.getElementById("brand"); var option = document.createElement("option"); option.text = "hello"; x.add(option) });