-2

I have the code below:

var allitems = "<option value=''>Choose an item</option>";
$.getJSON("http://localhost:59886/getItems", function (data) {
  var items = [];
  $.each(data, function (key, value) {
    if (parseInt(value.C_CODE) == parseInt($("#ccode").val())) { 
      allitems = allitems + "<option value='" + value.itemid+ "'>" + value.itemname + "</option>"; 
    };
  });
}, 'json').error(function (error) { console.log(error); });

When I run the code in Chrome console, I get values but when I ran it from a website, the allitems variable is empty. The JSON URL works fine.

Jacob
  • 77,566
  • 24
  • 149
  • 228
mpora
  • 1,411
  • 5
  • 24
  • 65

1 Answers1

0

Make sure you open the page on server. Check your URL and instead of local file, for example:

file:///D:/wamp/www/index.html

it should start with http protocol and localhost:

http://localhost/index.html

Chrome does not allow ajax requests on file url by default, though it may be changed.

Community
  • 1
  • 1
skobaljic
  • 9,379
  • 1
  • 25
  • 51